How is state being updated here?

The function

function handleChange(event) {
        const {name, value, type, checked} =
        setFormData(prevFormData => ({
            [name]: type === "checkbox" ? checked : value

updates the state of inputs, so for example email gets updated to value. But value is set to which is set to an empty string. So where is the value being derived from?

Here’s the link

Right, it starts out as an empty string, because that is what is in state. That is the initial state.

When you enter data in that field, say the letter “m”, the component calls handleChange. It calls setFormData which updates state. So, then equals “m”. State has changed so a render is triggered. When it gets to value={}, that now is “m” so that is what will be displayed.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.