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

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 :: how to send email 
Javascript :: find invalid json files in directory 
Javascript :: array object make api format javascript 
Javascript :: javascript stopped working 
Javascript :: cercle progress bar angular 
Javascript :: react native asyncstorage getitem 
Javascript :: firebase recaptcha using react js 
Javascript :: slice method javascript 
Javascript :: vscode redirect back 
Javascript :: complicated reduce 
Javascript :: detect escape characters js 
Javascript :: unable to append div multiple times 
Javascript :: how to get the total price of all product in cart using react 
Javascript :: Public properties can be created via Static public fields 
Javascript :: how we pass 2 args in switch case javascript 
Javascript :: typeorm sqlite Using Entity Manager 
Javascript :: custom validator Whitelisting 
Javascript :: last iteration is for loop js ES6 
Javascript :: javascript node retry promise.all 
Javascript :: react weather app 
Javascript :: const and let keywords in ES6 
Javascript :: selected item from dropdownlist 
Javascript :: angular material table generator 
Javascript :: react native red Triangle Down 
Javascript :: keep form values after submit javascript 
Javascript :: convert an array to other array 
Javascript :: @click:append 
Javascript :: how to add carsoul to react project 
Javascript :: provider not found react 
Javascript :: getelementbyid without the <script 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =