Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
When useState update then map loop is not working in array inside of object in useState. React js
import { useState } from "react";
import React from "react";
function Check() {
const [Children, setChildren] = useState({data:[], otherdata:{}});
function handleChange(){
Children["data"] = [...Children["data"], Children["data"].length]
setChildren(Children)
alert(Children["data"])
return (<React.Fragment>
<div>Check</div>
{Children["data"].map(data => <div>map</div>)}
<button
onClick={handleChange}
>Add List</button>
</React.Fragment>);
export default Check
–
const [Children, setChildren] = useState({ data: [], otherdata: {} });
function handleChange() {
setChildren({
...Children,
data: [...Children["data"], Children["data"].length],
return (
<React.Fragment>
<div>Check</div>
{Children.data.map((ele) => (
<div>{ele}</div>
<button onClick={handleChange}>Add List</button>
</React.Fragment>
export default Check;
Thanks for contributing an answer to Stack Overflow!
-
Please be sure to
answer the question
. Provide details and share your research!
But
avoid
…
-
Asking for help, clarification, or responding to other answers.
-
Making statements based on opinion; back them up with references or personal experience.
To learn more, see our
tips on writing great answers
.