Regarding the optional exercise on the last page, where we had to create a form for a school administrator.
I managed to get it to work, but I feel like it was very long and repetetive. For each of the 6 pieces of data we were asked for (first name, last name etc) I made an with corresponding , then a state and state setter, and event handler function. e.g.
then mapped over this to create a label and button for each one, but the trouble with this was (1) I couldn’t differentiate type=“text” and type=“number” and (2) I couldn’t set the value attribute as I needed it to be unique for each one. And even then, they all had the same event handler, so when I typed in one field, all fields updated.
My other thought was to initialise the state as an empty object and add the input to the object as the user typed, which worked to a point, but then I struggled with accessing the values within the object to be able to display. (As my keys were strings with multiple words, I couldn’t use dot notation, but bracket notation didn’t seem to work.)
How did other people tackle this? Thanks in advance!
Hello, i have the same issue as you do!
I va also repeated the function 5 times and it seems the its too much and that it should more automised solution… I dont really get how could you do that with an array thought…
Anyways Im happy to check the other answers if someone could resolved it!!
We didn’t learn this approach via the Intro to React lesson so I haven’t tried it yet, but it looks like you can use a single State hook for all values and then have an <input> tag for each value you want, specifying what type each input tag is.