Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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'
         }));
Comment

javascript react useState update object

- 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'
         }));
Comment

Update an object as state with React hooks

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"
/>
Comment

PREVIOUS NEXT
Code Example
Javascript :: hover vanilla javascript 
Javascript :: javascript go to page 
Javascript :: slug javascript 
Javascript :: prompt box to integer 
Javascript :: jquery add items to select input 
Javascript :: get epoch timestamp js 
Javascript :: how to change specific object in array javascript 
Javascript :: jquery get 
Javascript :: jquery get top position of element on scroll 
Javascript :: find year javascript 
Javascript :: javascript set and get cookie 
Javascript :: how to convert time to am pm in javascript 
Javascript :: jquery each 
Javascript :: hello is not defined javascript 
Javascript :: javascript text to speech 
Javascript :: scroll to bottom javascript 
Javascript :: string contains in react 
Javascript :: Remove all child nodes of a list: 
Javascript :: mongoose virtual populate not working 
Javascript :: jquery select option value id no not exists 
Javascript :: how to create a random 2d vector in js 
Javascript :: react native image disable fade in onload 
Javascript :: urlencoded json express 
Javascript :: error placement jquery validation 
Javascript :: textalign javascript 
Javascript :: jQuery search immediate children 
Javascript :: vue call function every x seconds 
Javascript :: how to fix eslint error no-prototype-builtins 
Javascript :: jquery right click 
Javascript :: cypress type force 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =