My object is create an array of object as follows [{},{},{}]
State variable constructor like this:
this.state:{...some states,
parsed:[{}]}
Every object come from get IPFS with async function:
IPFSREADER = element => {
ipfs.cat(convertedIPFSaddress).then(result => {
let cons = result.toString('utf8')
const consdata = JSON.parse(cons);
// NEED useState save consdata to parsed state
}
When I get data, I am pushing this data to state with useState, but I tried as follows way:
this.setState({parsed:consdata})result:> Object:{some data...}this.setState({...[parsed],parsed:[consdata]})result:[0:Object:{some data...}]- When there is a new data coming from ipfs, this only changes data, where is into the array, but doesn't append to tail of previous data.
this.setState(parsed => [...data, parsed]);follow Joseph D.'s answer, but throwReferenceError: can't access lexical declaration 'data' before initializationerror. Try this solution changed above function as follows:- changed the function to async:
IPFSREADER = async element => {.. - added await in front of ipfs.cat:
const { data } = await ipfs.cat(...
- changed the function to async:
How can solve this problem? I tried many solutions, but can't change the result... Thanks.