Take a look at the code below,
Question is, Why a new list item is not getting added on click of "Add to List" ?
function SuperList(props) {
const liVals = [{name:"One"}, {name:"Two"}, {name:"Three"}, {name:"Four"}, {name:"Five"}];
const [liValues, up] = useState(liVals);
function AddToList() {
liValues.concat({name:"Six"});
liValues.push({name:"Six"});
alert(liValues.map(el => " " + el.name));
up(liValues);
}
return (<>
<ul>
{liValues.map((l) => <li>{l.name}</li>)}
</ul>
<button onClick={AddToList}>Add to List</button>
</>
);
}
createRoot(document.getElementById('root')).render(
<>
<SuperList/>
</>
);
3
Answers
More information about mutation HERE