i would like to know how can i set one state with few inputs value for the example this is the state
const [userBirthday, setUserBirthday] = useState('')
there are 3 inputs, i want to get e.target.value from each one and conbain them in the state
something like that **I know the syntax actually not good but it is only to simplify my question
<input type="text"
placeholder='Day'
onChange={(e) => {
setUserBirthday(userBirthday += e.target.value)
}}
/>
<input type="text"
placeholder='Day'
onChange={(e) => {
setUserBirthday(userBirthday += e.target.value)
}}
/>
<input type="text"
placeholder='Day'
onChange={(e) => {
setUserBirthday(userBirthday += e.target.value)
}}
/>
`
console.log(userBirthday) == Result: 12 September 2020
This function will do the job, you need to add the handleChange method to all the input field's prop "onChange" and you need to add the name prop to all the input fields, I added input1 for the demo.
const handleChange = (e) => {
const data = { ...userBirthday };
data[e.currentTarget.name] = e.currentTarget.value;
setUserBirthday(data);
};
<input type="text"
name="input1"
placeholder='Day'
onChange={handleChange}
/>