I'm trying to use splice to add new components
into an array. If I use concat
all the elements are added properly at the end, but what I also need is add at the beginning or in the middle of the array using splice
. Any suggest ?
class App extends React.Component {
state = {
components: []
};
addNewElement = (element) => {
this.setState(prevState => ({
//Works fine
//components: prevState.components.concat(element)
components: prevState.components.splice(0, 0, element)
}));
};
}