reactjs - How to refresh props with React/Redux when user enters a container -


i have competitionsection repeats competitions database. when user clicks on one, redirects him competition page, loads second , renders page details in it. far, good.

but when users goes competition section , click on second competition, instantly loads previous competition, 0 loading time.

from point of view, failing props of component not updating when render component (from second time). not router problem, first instinct because i'm seeing route.params changing acordingly, actions dispatch change props not dispatching. here's bit of code of said component.

class competitionpage extends react.component {  componentwillmount() {   let id = getidbyname(this.props.params.shortname)   this.props.dispatch(getcompaction(id));   this.props.dispatch(getcompmatches(id));   this.props.dispatch(getcompparticipants(id));   this.props.dispatch(getcompbracket(id)); }  render() {     let { comp, compmatches, compbracket, compparticipants } = this.props  ... 

i tried every lifecycle method know. component will/did mount, component will/did update , set shouldupdate true , didn't trick. understand, problem solved lifecycle method dispatch actions everytime user enters competition page , not first time. i'm running out of options here, appreciated.

note: i'm newbie @ react/redux know there couple of things there anti-pattern/poorly done.

update: added competitionssection

 class competitionssection extends react.component {     render() {        const {competitions} = this.props;           return (            ...            { object.keys(competitions).map(function(comp, i) {            return (                 <div key={i} classname={competitions[comp].status ===                 undefined? 'hide-it':'col-xs-12 col-md-6'}>                ...                <link to={"/competitions/"+competitions[comp].shortname}>                    <raisedbutton label="ver torneo" primary={true} />                </link>                ... 

it helps better understand lifecycle hooks. mounting component when placed on dom. can happen once until removed dom. update occurs when new props passed or setstate called. there few methods troubleshoot when updates not happening when think should:

  1. ensure changing state in componentdidmount or componentdidupdate. cannot trigger update in componentwillmount.

  2. make sure new props or state new objects. if passing object down in props , mutating object, not trigger update. instance, not trigger update:

class competitionpage extends react.component {   constructor(props) {     super(props)      this.state = {       competitions: [ compa, compb ]     }   }    triggerupdate() {     this.setstate({       competitions: competitions.push(compc)     })   }  componentdidmount() {   triggerupdate() }  render() {   return(     <div>       hello     </div>   ) } 

this due fact new competition being appended array in state. correct way completly create new state object , change needs changed:

const newcompetitions = this.state.competitions.concat(compc)  this.setstate(object.assign({}, this.state, { competitions: newcompetitions })) 
  1. use componentwillrecieveprops on update compare previous , current prop values. can setstate here if clean needs done:

read more method in react documentation:

https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -