I'm looking to find a way to add an array of objects to a useState hook. My approach works with an array of strings but not with an array of objects
const [info, setInfo] = useState([{}])
let names = [{name: "george"}, {name: "bill"}, {name: "adam"}]
setInfo((oldArray) => [...oldArray, names])
CodePudding user response:
You have to deconstruct names
too.
you are currently setInfo like
[{},[{name: "george"}, {name: "bill"}, {name: "adam"}]]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
CodePudding user response:
I was able to solve the issue by adding a spread operator for names as well
setInfo((oldArray) => [...oldArray, ...names])