I have an array of schoolClubs that contains 2 separate school clubs. I kept the logic simple. Each club array list the students in the very club.
I set the club index by the club state so that it will show the current club students. ıt starts with 0. By that club index, I create another students state and using it to create JSX.
When Change Club button is clicked, it is supposed to increment club state so that I can list the second club. For Line 15, the code works but is it correct to change the students state by the raw "club" state. I am nervous club state may not be current because I am incrementing it by a callback 1 line above?
I even tried to setStudents in setClub callback: https://playcode.io/1470340
I am not sure which one is correct way in React?
BTW, this is simplified code, I need to have club and students as states because I add or delete students from the students array and refresh the display. But when Change Club button clicked, it needs to increment club index and list the next default students from the schoolClubs array.
Try this online React Playground playground with instant live preview and console. Easy & Fast. Experiment yourself.
Try this online React Playground playground with instant live preview and console. Easy & Fast. Experiment yourself.