Search code examples
sharepointreact-hookshooksharepoint-onlinespfx

how to read the state when starting the software?(UseState and UseEffect)


** when I start the software for the first time the "items" state property is not set. Not even in setItems. If I try to reload a second time instead it is valued. How can I change the following code to make the status value also on first opening?**

(https://i.sstatic.net/8bgSP.png)](https://i.sstatic.net/8bgSP.png)

How can I change the following code to make the status value also on first opening?


Solution

  • enter code hereI am still learning React Hooks, but I think I see the issue. In your useEffect function, you populate items by calling the _getItemButton function, but then you immediately setItems back to an empty array. Remove the setItems([]); statement in the useEffect function and see if that fixes the issue.

    By including the empty array, the useEffect function only runs the first time. Clicking the button runs the _getItemButton function again directly, not using the useEffect function, so that is why it works the second time.

    Hope this helps!