reactjs - Should I call actions within componentWillReceiveProps? -


my instinct tells me no, i'm having difficultly thinking of better way.

currently, have component displays list of items. depending on provided props, list may change (i.e. filtering change or contextual change)

for example, given new this.props.type, state updated follows:

componentwillreceiveprops(nextprops) {         if (nextprops.type == this.state.filters.type) return          this.setstate({             filters: {                 ...this.state.filters,                 type: nextprops.type,             },             items: itemsstore.getitems().filter(item => item.type == nextprops.type)         }) } 

this fine , good, requirements have changed , need add new filter. new filter, must execute api call return list of valid item ids , want display items these ids in same list component. how should go this?

i had thought calling appropriate action componentwillreceiveprops, doesn't seem right.

componentwillreceiveprops(nextprops) {         if (nextprops.type == this.state.filters.type && nextprops.otherfilter == this.state.filters.otherfilter) return          if (nextprops.otherfilter != this.state.filters.otherfilter) {             itemsactions.getvalididsforotherfilter(nextprops.otherfilter)             // items updated in store change listener, onstorechange below         }          this.setstate({             filters: {                 ...this.state.filters,                 type: nextprops.type,                 otherfilter: nextprops.otherfilter,             },             items: itemsstore.getitems().filter(item => item.type == nextprops.type)         }) },  onstorechange() {      let validids = itemsstore.getvalidids()       this.setstate({          items: itemsstore.getitems().filter(item => item.type == this.state.filters.type && validids.indexof(item.id) > -1)      }) } 

let's have @ options:

componentdidmount

if need api call when component mounts, option.

componentwillmount

is executed server side in rendertostring. api call in here can unwanted.

shouldcomponentupdate

should used deciding if component should re-render or not, no side effects api calls.

componentwillupdate

setstate not possible in method anymore. if you're doing api call you're going show loading indicator or @ least resetting current items guess. not right hook.

componentdidupdate

would possible, if you're going reset current items or show loading indicator, render necessary. first render unnecessary. linters complain if use setstate in here reason. trigger infinite loop if you're unconditionally using setstate every time lifecycle method being executed.

componentwillunmount

definitely not right thing.

componentwillreceiveprops

very best option. if want reset items or show loading indicator, using setstate in here won't trigger additional render. avoid generating state based on props. however, there cases necessary (like yours). lifecycle method way ensure state date.


hope helps!


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -