Skip to content

Update object state #17

@will83

Description

@will83

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions