Home > front end >  How to set a value of nest object value in onChange event
How to set a value of nest object value in onChange event

Time:08-24

I'm trying to set a value of nested object value for the onChange event.

const [state, setState] = useState({
            fullname: '',
            email:
            address: {
                fhbca: '',
                street: '',
                landmark: '',
                pincode: '',
            }
        });

onChange={(e) => { setState({...state, address.fhbca : e.target.value }) }} **<--**

When setting the value of address.fhbca. Using this above statement throws errors Accounts.jsx: Unexpected token, expected "," (120:100). Please correct me and suggest. how to set a value of this address.fhbca ..?

CodePudding user response:

You need to do like this

onChange={(e) => { setState(({...state, address: {...state.address, fhbca: e.target.value} }})}}
  • Related