I am new to working with reducers to store data and have stumbled across an issue. I currently have a list of checkboxes that when clicked dispatches an action to store them inside an array in my reducer. Currently, however, I can keep checking/unchecking and the same value will continue to be pushed to the array.
How can I prevent this from happening? Do I somehow filter out inside the reducer or in my handleChange event on the checkbox?
I have added a small snippet of my current code
Thanks!
function handleCheckboxChange(e) {
if (e.target.checked) {
checkboxContext.dispatch({
type: 'SET_PROPERTY_TYPE',
payload: { [e.target.name]: e.target.checked }
});
}
}
const FilterCheckbox = ({ name, value, handleChange, checkedItems }) => (
<Label>
<FilterInput
type="checkbox"
name={name}
value={value}
onChange={handleChange}
checked={checkedItems[name]}
/>
{name}
</Label>
);
export default FilterCheckbox;
// Sets the selected value...
case 'SET_PROPERTY_TYPE':
return {
...state,
propertyType: [...state.propertyType, action.payload]
};