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.
- you should doing in
componentdidmount
no need check ifismounted
- if specify
datatype
jquery json parse , data object - 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
Post a Comment