Q:

React setState only if thing exists?

So I am setting up a parent with several state attributes to pass to children. This is being done via AJAX. The trouble is that for some of these variables, the response may return null or undefined.
$.post(this.props.source, function(data) {

  // Parse the response
  var response = JSON.parse(data);

  if (this.isMounted()) {
    this.setState({

      // Set initial state vars
      minPrice:         response.resultDetails.minPrice,
      maxPrice:         response.resultDetails.maxPrice,
      language:         response.params.language,
      filters:          response.params.filters

    });
  }
}.bind(this));

How can I say something like if response.params.language, set language? I cannot figure out the React syntax to do such a thing.

A:

You should be doing this in componentDidMount so no need to check if isMounted
If you specify the dataType then jQuery will do a json parse for you and data will already be an object
If response.params.language is falsy (undefined), why would it matter if you set it - because you're overriding a non-falsy value?

You can do this in many vanilla JS ways.. this isn't related to React. e.g:
var newState = {
  minPrice: response.resultDetails.minPrice,
  maxPrice: response.resultDetails.maxPrice,
};

if (response.params.language) {
  newState.language = response.params.language;
}

if (response.params.filters) {
  newState.filters = response.params.language;
}

this.setState(newState);

or:
this.setState({
      minPrice: response.resultDetails.minPrice,
      maxPrice: response.resultDetails.maxPrice,
      language: response.params.language || this.state.language,
      filters: response.params.filters  || this.state.filters
});

