I am following the advice given in this Stack Overflow answer: Trying to Reset a group of Radio button in REACT through a button
But, I cannot get it to work.
I am developing a simple blog webapp for practice and I want to have a radio button on the "Users" UI to specify whether a user has admin privileges or not.
Here are some screenshots of the UI and Chrome Developer Console:
Here is my code:
This is code from my EditUser()
// useState for the isAdmin radio buttons
const [adminIsTrue, checkedState] = useState(null);
const getRadioProps = useCallback((id, value) => {
return {
id,
value,
type: "radio",
name: "isAdmin",
checked: adminIsTrue === value,
onChange: () => checkedState(value)
};
}, [adminIsTrue]);
This is code from my return():
{/* Admin Priveledges */}
<div>
<div className="radiobtn">
<label htmlFor='body'>This user has admin priveledges?</label>
<br></br>
<input {...getRadioProps("true", 1)} />
<label htmlFor="true">True</label>
<div className="radiobtn">
<input {...getRadioProps("false", 0)} />
<label htmlFor="false">False</label>
</div>
</div>
</div>
Both radio buttons give me a developer console output of '1'.
Do any of you know how to get this code working so that the value from my database is reflected in the radio buttons (i.e if isAdmin is "true" in my database, I want the "true" radio button to be checked, but I want to be able to change the radio button values by clicking on them)?
Any ideas?
Kind Regards,
jm03 Melbourne, Australia
I have tried a few methods and none seem to work.