I have the following object.
const data = [
{
key: '1',
name: 'John Brown',
date: 'Mon Oct 31 2013 00:00:00 GMT-0700 (PDT)',
address: 'New York No. 1 Lake Park',
},
{
key: '2',
name: 'Joe Black',
date: 'Mon Oct 31 2014 00:00:00 GMT-0700 (PDT)',
address: 'London No. 1 Lake Park',
},
{
key: '3',
name: 'Jim Green',
date: 'Mon Oct 31 2011 00:00:00 GMT-0700 (PDT)',
address: 'Sidney No. 1 Lake Park',
},
{
key: '4',
name: 'Jim Red',
date: 'Mon Oct 31 2010 00:00:00 GMT-0700 (PDT)',
address: 'London No. 2 Lake Park',
},
];
And this state
const [usersData, setUsersData] = React.useState([]);
I'm trying to setup its data with an useEffect on component mount.
React.useEffect(() => {
setUsersData(result => [...result, data[0]])
props.setLoading(false);
}, []);
But im having an issue with the setUsersData
Argument of type '(result: never[]) => { key: string; name: string; date: string; address: string; }[]' is not assignable to parameter of type 'SetStateAction<never[]>'. Type '(result: never[]) => { key: string; name: string; date: string; address: string; }[]' is not assignable to type '(prevState: never[]) => never[]'. Type '{ key: string; name: string; date: string; address: string; }[]' is not assignable to type 'never[]'. Type '{ key: string; name: string; date: string; address: string; }' is not assignable to type 'never'.ts
UPDATE:
This is what I have currently.
const data = .... is still the same
I changed the useState to
const [usersData, setUsersData] = React.useState<any[]>([]);
And I do this now
React.useEffect(() => {
setUsersData(result => [...result, data[0]])
props.setLoading(false);
}, []);
But this way it only adds the elemtn 0 to my array, and Im trying to add all the elements contained in data
setUsersData([...userData, data[0]])
– wangdev87Argument of type 'any[]' is not assignable to parameter of type 'SetStateAction<never[]>'. Type 'any[]' is not assignable to type 'never[]'. Type 'any' is not assignable to type 'never'.
– mouchin777const [usersData, setUsersData] = React.useState<any[]>([]);
– wangdev87setUsersData(result => [...result, data[0]])
. What are you trying to add to the state? – yudhiesh