I can't remove specific elements of array by index number from any dynamic div
const { useState } = React;
function Check(){
var [Children, setChildren] = useState([])
function RemArr(docs){
const temp = [...Children]
temp.splice(docs["i"], 1);
setChildren(temp)
}
function Product(docs) {
var document = (<React.Fragment>
<button onClick={()=>RemArr(docs)}>
List {docs["i"]}
</button>
<p></p>
<input/>
<p></p>
</React.Fragment>);
setChildren([...Children, document])
}
return (<React.Fragment>
<div>Check</div>
{Children}
<button
onClick={()=>{Product({fix:false, i:Children.length})}}
>Add List </button>
</React.Fragment>)
}
ReactDOM.render(<Check />, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.0/umd/react-dom.production.min.js"></script>
When i want to remove any specific div then not only this div remove from array, also remove next div from array.
Eg.
array = [0,1,2,3,4,5,6,7,8,9]
If I want to remove only 6 from the array, however, it instead removes all the values from 6 to 9, and so I am left with just:
array = [0,1,2,3,4,5]
I think the problem originates from this function:
function RemArr(docs){
const temp = [...Children]
temp.splice(docs["i"], 1);
setChildren(temp)
}