react-context-hook icon indicating copy to clipboard operation
react-context-hook copied to clipboard

Update object state

Open will83 opened this issue 5 years ago • 0 comments

I came from a standard useState hook and my code was working properly. Now, I can't update my state (the custom_filters object) with the SetFilters method below

const initialState = { 
  custom_filters: {
    blue: false,
    green: false,
    pink: false
  }
}
const [filters, setFilters, deleteFilters] = useStore('custom_filters');

const CustomSwitch = (props) => {
  const paramId = props.id;
  const paramValue = filters[paramId];
  
  return(
    <Switch
      value={paramValue}
      onValueChange={value => {
        setFilters(prevState => ({ ...prevState, [paramId]: value }));
      }}
    />
  )
}

{...}
<CustomSwitch id="blue" />

This is working : setFilters({[paramId]: value }); but I need to keep all my items inside custom_filters.

There is something wrong with my code? Thank you for your help

will83 avatar May 28 '20 18:05 will83