0

I want to set state of this form :

this.state = {
  filter: {
    search: null,
    brands: null,
    price: null
  }
}

How to set value for search / brands / price ?

8 Answers 8

2

Do the following:

this.setState({
  filter: {
    search: 'value',
    brands: 'value',
    price: 'value'
  }
})
Sign up to request clarification or add additional context in comments.

Comments

2

The key is that you don't want to ever mutate a value in state. As a result, you must copy the filter object before passing it to setState. Example:

onSearchChange(value) {
  this.setState((state) => {
    return {
       filter: {
         ...state.filter,
         search: value
       }
  })
}

Note that I am passing a function to setState. Since the next value of state relies on the previous value, you want to use an updater functions, as the setState docs recommend.

In general, it is nicer if you can keep your state flat. So rather than having a filter object in state, your shape could just be

this.state = {
   search: null,
   brands: null,
   price: null,
}

In which case the above onSearchChange function would just be

onSearchChange(value) {
  this.setState({search: value})
}

Definitely a lot easier on the eyes.

Comments

2

I recommend avoiding nested objects and keeping your state flat. e.g.

this.state = {
  brandsFilter: null,
  priceFilter: null,
  searchFilter: null,
};

Component state in react is really nothing more than simple key-value pairs; that's what setState supports. Sure you can have nested objects if you really want. But as the other answers demonstrate, supporting such an approach can be needlessly complex.

Comments

1

you should use the setState function, you can set the filter with updated data like so

const newFilter = {...};
this.setState({filter: newFilter});

Comments

1

You should avoid to mutate React state directly, there are some functions can do immutable jobs for you (ex Object.assign):

const currentFilter = this.state.filter;
const newFilter = Object.assign({}, currentFilter, {search: "new search", brands: [], price: 100});
this.setState({filter: newFilter});

ES 6:

const currentFilter = this.state.filter;
this.setState({
   filter: {
      ...currentFilter,
      search: "new search",
      brands: []
   }
});

Comments

1
this.setState({
filter: {
      ...this.state.filter,
      search: "new search", 
      brands: 'value',
      price: 'value'
   }
});

You may try this with Spread Operator. If you need to preserve the previous filter value. other wise you can,

this.setState({
    filter: {
          search: "new search", 
          brands: 'value',
          price: 'value'
       }
    });

Comments

1
let newfilter = Object.assign({}, this.state.filter)

newfilter.search ="value";
newfilter.brands ="value";
newfilter.price ="value";

this.setState({
    filter:newfilter
})

Comments

-5

You can access the search, brands, price by using:

this.setState({
    filter.search = true,
    filter.brands = 'stackoverflow',
    filter.price = 1400
})

and to access it, just like usual state access (this.state.filter.search).

2 Comments

You do not want to directly mutate state like this in React.
avoid mutate straight to react state

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.