how to change one single key value in state
TypeError
Cannot read properties of null (reading ‘value’)
import React, { useState } from "react";
const options = ["cricket", "football", "hockey"];
const days = ["weekday", "weekend"];
function App() {
const [otp, setOtp] = useState({ opts: "", das: "" });
const chng = event => {
setOtp(prevValues => {
return { ...prevValues, opts: event.target.value };
});
};
const chgd = event => {
if (event.target && event.target.value) {
setOtp(prevValues => {
return { ...prevValues, das: event.target.value };
});
}
};
return (
<div className="App">
<h1>Which game</h1>
{options.map(value => (
<div key={value}>
<input
type="radio"
name="options"
id={value}
value={value}
onChange={chng}
/>
<label htmlFor={value}>{value}</label>
</div>
))}
<h1>Which Day</h1>
{days.map(value => (
<div key={value}>
<input
type="radio"
name="days"
id={value}
value={value}
onChange={chgd}
/>
<label htmlFor={value}>{value}</label>
</div>
))}
<p>Will Play {otp.opts} on {otp.das}</p>
</div>
);
}
export default App;
3
Answers
Adding
event.persist()
to the beginning of each event handler fixes the problem:This is because versions of React before 17 do something called event pooling, which sets event fields to
null
and attempts to reuse the object for performance in older browsers.This answer explains the problem in more detail.
I think the problem comes when you set the
setOtp
function, try doing it this way and it works fineinstead of
you can do this
and you can make the same in
chng
function:There is an easier way to think about what you are doing .. I’ve severely dumbed it down so that you can understand the setting and getting. I’ve removed the object from the state, and set two different states. This is just another way to look at it. This isn’t the way I would do it .. But I wanted you to see the function based code, in a ELI5 way, so you can see it working and all it’s individual pieces.
Note, if you don’t need ANYTHING ELSE to happen in your
chng()
functions .. You can also just straight set the state from theonChange=
event IE:without the need for external functions.