admin管理员组文章数量:1406312
I am trying to use useState
in my react functional ponent to update array of objects but nothing is happening. Here is the code I am trying:
const countriesData = [
{ id: 1, name: "India", person: "Harpreet" },
{ id: 2, name: "Aus", person: "Monty" },
{ id: 3, name: "UK", person: "Preet" },
];
ReactDom.render(<App />, document.getElementById("root"));
function App() {
const [name, setName] = useState(countriesData);
const arr = [{ id: name.length + 1, name: "Pak", person: "Mnm" }];
const AddMe = () => {
setName({ ...name, ...arr });
};
return (
<>
<CountryName data={countriesData} />
<button onClick={AddMe}> Add me</button>
</>
);
}
function CountryName({ data }) {
return (
<React.Fragment>
{data.map((element, index) => (
<div key={index}>
<h1>{element.person}</h1>
<span>from : {element.name}</span>
</div>
))}
</React.Fragment>
);
}
I am a newbie in React. I also tried having prevState
as setName
argument and then add like setName(prevState{ ...pevState, ...arr });
Already had a look on so many sources but no luck. Any help will be appreciated. Thanks!
I am trying to use useState
in my react functional ponent to update array of objects but nothing is happening. Here is the code I am trying:
const countriesData = [
{ id: 1, name: "India", person: "Harpreet" },
{ id: 2, name: "Aus", person: "Monty" },
{ id: 3, name: "UK", person: "Preet" },
];
ReactDom.render(<App />, document.getElementById("root"));
function App() {
const [name, setName] = useState(countriesData);
const arr = [{ id: name.length + 1, name: "Pak", person: "Mnm" }];
const AddMe = () => {
setName({ ...name, ...arr });
};
return (
<>
<CountryName data={countriesData} />
<button onClick={AddMe}> Add me</button>
</>
);
}
function CountryName({ data }) {
return (
<React.Fragment>
{data.map((element, index) => (
<div key={index}>
<h1>{element.person}</h1>
<span>from : {element.name}</span>
</div>
))}
</React.Fragment>
);
}
I am a newbie in React. I also tried having prevState
as setName
argument and then add like setName(prevState{ ...pevState, ...arr });
Already had a look on so many sources but no luck. Any help will be appreciated. Thanks!
-
name
is an array. InAddMe
you are trying to merge objects – user0101 Commented Aug 31, 2020 at 12:43 - You are changing state property name, but you are always looping over countries data. Did you intend to loop over property name? – Alexander Staroselsky Commented Aug 31, 2020 at 12:49
2 Answers
Reset to default 4The initial data is an array, see in countriesData
variable, also in setName
you try to use {}
.
You should call with []
instead of {}
as:
setName([ ...name, ...arr ]);
You need to do:
setName([ ...name, ...arr ]);
or
setName((prev) => [...prev, ...arr]);
and also update data:
<CountryName data={name} />
Here is full code:
import React, { useState } from "react";
import "./styles.css";
const countriesData = [
{ id: 1, name: "India", person: "Harpreet" },
{ id: 2, name: "Aus", person: "Monty" },
{ id: 3, name: "UK", person: "Preet" }
];
function CountryName({ data }) {
return (
<React.Fragment>
{data.map((element, index) => (
<div key={index}>
<h1>{element.person}</h1>
<span>from : {element.name}</span>
</div>
))}
</React.Fragment>
);
}
export default function App() {
const [name, setName] = useState(countriesData);
const arr = [{ id: name.length + 1, name: "Pak", person: "Mnm" }];
const AddMe = () => {
setName((prev) => [...prev, ...arr]);
};
return (
<>
<CountryName data={name} />
<button onClick={AddMe}> Add me</button>
</>
);
}
Here is the demo: https://codesandbox.io/s/keen-buck-ue9sn?file=/src/App.js:0-826
本文标签: javascriptReact useState not updating array of objectsStack Overflow
版权声明:本文标题:javascript - React useState not updating array of objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745005976a2637265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论