Answers for "how to update an object in hookstate"

6

update object in react hooks

- Through Input

        const [state, setState] = useState({ fName: "", lName: "" });
        const handleChange = e => {
            const { name, value } = e.target;
            setState(prevState => ({
                ...prevState,
                [name]: value
            }));
        };

        <input
            value={state.fName}
            type="text"
            onChange={handleChange}
            name="fName"
        />
        <input
            value={state.lName}
            type="text"
            onChange={handleChange}
            name="lName"
        />
   ***************************

 - Through onSubmit or button click
    
        setState(prevState => ({
            ...prevState,
            fName: 'your updated value here'
         }));
Posted by: Guest on January-02-2021
0

React update state array of objects hooks

React fin and update state value
Posted by: Guest on May-18-2021

Code answers related to "how to update an object in hookstate"

Code answers related to "Javascript"

Browse Popular Code Answers by Language