Skip to content
Advertisement

Update state array in React JS with item from Smart Contract

I have a state array in react which I am trying to add items from a smart contract

The state array is initialised as

const [products, setProducts] = useState([]);

  1. and 2. below did not work, but 3. does work when the array is set outside the loop

What am I doing wrong in 1. and 2?

——————————————————————

  1. did not work
JavaScript

——————————————————————

  1. did not work
JavaScript

——————————————————————

  1. worked
JavaScript

——————————————————————

Advertisement

Answer

You’re using a functional component, not a class component, so you shouldn’t be using this or this.state – that’s why (1) doesn’t work.

(2) doesn’t work because your initial products are a plain array – it’s not an object with a products property:

JavaScript

so

JavaScript

doesn’t make sense – prevState is an array, not an object with a products property.

With a small tweak, (2) could be made to work:

JavaScript

There’s also almost certainly no need to use .call, and you can speed up the process by using Promise.all instead of waiting for each function to finish individually:

JavaScript

That’s the version I’d prefer, if your code permits you to make all the requests at once.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement