0

I have a piece of state which looks as so:

    const [values, setValues] = useState({
        firstName: '',
        lastName: '',
        dateOfBirth: {
            month: '',
            day: '',
            year: ''
        },
        phoneNumber: '',
        gender: '',
        interests: [],
        language: '',
        medicareNumber: ''
    })

I try to reassign the values of month day and year in the dateOfBirth object onChange, what is the syntax to do this and not overwrite any of the other keys in the object?

                        <input
                            value={values.dateOfBirth.month}
                            onChange={(e) => setValues({
                                ...values,
                                dateOfBirth: { month: e.target.value }
                            })}
                            type='number'
                            placeholder='MM'
                        />

                        <input
                            value={values.dateOfBirth.day}
                            onChange={(e) => setValues({
                                ...values,
                                dateOfBirth: { day: e.target.value }
                            })}
                            type='number'
                            placeholder='DD'
                        />

the code above is setting the value of dateOfBirth to only month or only day. But I need all the keys and values to remain the same and only change the value inside the respected key.

3
  • You can spread dateOfBirth inside itself before overwriting the key value pair you want Commented Nov 19, 2021 at 19:39
  • @Reyno I figured it is possible. I just can't dial in the correct syntax. may you provide a code example? Commented Nov 19, 2021 at 19:42
  • For complex state it's recommended to use useReducer. You have more control over the state. useState is generally used for simple state variable like primitive values. en.reactjs.org/docs/hooks-reference.html#usereducer Commented Nov 19, 2021 at 20:08

1 Answer 1

1

Try:

<input
    value={values.dateOfBirth.month}
    onChange={(e) => setValues({
        ...values,
        dateOfBirth: {
            ...values.dateOfBirth,
            month: e.target.value
        }
    })}
    type='number'
    placeholder='MM'
/>

Or better, try not to have nested state properties.

Sign up to request clarification or add additional context in comments.

2 Comments

I have 3 separate inputs for day month and year. It was the only way I could figure to do it? Any better solutions?
1. use <input type="date">; 2. create a <DatePicker> component that returns a Date object, and store that inside your main form state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.