'Clear a list of checkboxes in React
I have a few programatically generated lists of checkboxes (uncontrolled) in my project, and I'd like a react-way to clear them all at the click of a button. My problem is, I have no way to reference them each.
I've tried setting refs but I can't seem to figure out how to share refs between a stateless child and it's parent (is this even possible?)
All I need is a button that, when clicked, unchecks all checkboxes. I never would have imagined this would be so complicated but I've been at it for an hour now and my head is starting to spin. Here is an example of one of the lists:
{props.filters[0].map(jobType => (
<li key={_.uniqueId('subType_')} className="checkbox text-sm m-b-sm">
<label className="">
<input type="checkbox" value={jobType.value} name="subtype" onChange={props.handleFilterCheck} />
<span>{jobType.title}</span>
</label>
<span className="text-muted"></span>
</li>
))}
So I have a way to consistently reference the size of the list and I know I could use iteration if I just had a way to reference the list but as it stands now that isn't possible. Any ideas?
Solution 1:[1]
I managed to solve the same question by creating list of <Checkbox /> components. Then at parent level I used array state, which held all checked values. And one of the props to <Checkbox /> components was boolean which was telling if parent array state is empty.
const Parent = (someList) => {
const [checkedArray,setCheckedArray] = useState([]);
return(
someList.map(el => <Checkbox key={el}
empty={checkedArray.length === 0}
value={el.value} />
);
);
};
const Checkbox = ({empty, value}) => {
const [checked, setChecked] = useState(false);
useEffect(() => {
empty && setChecked(false);
}, [empty]);
return(
//some checkbox JSX
);
};
I'm newbie so there might be some pitfalls.
Solution 2:[2]
I came for this same problem, did a bit of thinking, and figured out a more "React" way.
I'm using checkboxes as a way to enter query parameters to call an api. We can use state to keep track of which checkboxes are currently checked. However, when rendering these checkboxes, we can set the checked attribute to be programmatically connected to our state. If the checkbox's value is in state, check it. Otherwise, don't.
We can then set up a button to reset this piece of state on click. Because state changes re-render the component, all of the checkboxes would re-render - setting checked to false.
const [selectedBoxes, setSelectedBoxes] = useState([]);
const renderCheckboxes = (items) => {
return items.map(item => (
<div key={item.id}>
<input value={item.id} onChange={handleCheckbox} name="categories" checked={selectedBoxes.includes(item.id)} />
</div>
)
}
const handleCheckbox = (e) => {
setSelectedBoxes([...selectedBoxes, e.target.value]);
}
return (
<div>
{renderCheckboxes()}
<button onClick={setSelectedBoxes([])}>Click Me</button>
</div>
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fcdt |
| Solution 2 | jlewis90 |
