the state is empty when adding objects with usestate

ArrowForm is reloading the page when the submit button is pressed. You should prevent default form action on the onSubmit event object.

<form
  onSubmit={(e) => {
    e.preventDefault();
    newArrow([...arrows, <Arrow />]);
  }}
>

I suggest also to use functional state updates so state updates correctly from the previous state.

ArrowForm.js

<form
  onSubmit={(e) => {
    e.preventDefault();
    newArrow(arrows => [...arrows, <Arrow />]);
  }}
>

App.js

<button onClick={() => createArrow(arrows => [...arrows, <Arrow />])}>
  Add Force
</button>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top