javascript - React setState only if thing exists? -


so setting parent several state attributes pass children. being done via ajax. trouble of these variables, response may return null or undefined.

$.post(this.props.source, function(data) {    // parse 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 if response.params.language, set language? cannot figure out react syntax such thing.

  1. you should doing in componentdidmount no need check if ismounted
  2. if specify datatype jquery json parse , data object
  3. if response.params.language falsy (undefined), why matter if set - because you're overriding non-falsy value?

you can in many vanilla js ways.. isn't related 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 }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -