score:148

Accepted answer

you have to add a condition in your componentdidupdate method.

the example is using fast-deep-equal to compare the objects.

import equal from 'fast-deep-equal'

...

constructor(){
  this.updateuser = this.updateuser.bind(this);
}  

componentdidmount() {
  this.updateuser();
}

componentdidupdate(prevprops) {
  if(!equal(this.props.user, prevprops.user)) // check if it's a new user, you can also use some unique property, like the id  (this.props.user.id !== prevprops.user.id)
  {
    this.updateuser();
  }
} 

updateuser() {
  if (this.props.ismanager) {
    this.props.dispatch(actions.fetchallsites())
  } else {
    const currentuserid = this.props.user.get('id')
    this.props.dispatch(actions.fetchuserssites(currentuserid))
  }  
}

using hooks (react 16.8.0+)

import react, { useeffect } from 'react';

const sitestablecontainer = ({
  user,
  ismanager,
  dispatch,
  sites,
}) => {
  useeffect(() => {
    if(ismanager) {
      dispatch(actions.fetchallsites())
    } else {
      const currentuserid = user.get('id')
      dispatch(actions.fetchuserssites(currentuserid))
    }
  }, [user]); 

  return (
    return <sitestable sites={sites}/>
  )

}

if the prop you are comparing is an object or an array, you should use usedeepcompareeffect instead of useeffect.

score:0

you could use the getderivedstatefromprops() lifecyle method in the component that you want to be re-rendered, to set it's state based on an incoming change to the props passed to the component. updating the state will cause a re-render. it works like this:

static getderivedstatefromprops(nextprops, prevstate) {
  return { mystateproperty: nextprops.myprop};
}

this will set the value for mystateproperty in the component state to the value of myprop, and the component will re-render.

make sure you understand potential implications of using this approach. in particular, you need to avoid overwriting the state of your component unintentionally because the props were updated in the parent component unexpectedly. you can perform checking logic if required by comparing the existing state (represented by prevstate), to any incoming props value(s).

only use an updated prop to update the state in cases where the value from props is the source of truth for the state value. if that's the case, there may also be a simpler way to achieve what you need. see - you probably don't need derived state – react blog.

score:1

a friendly method to use is the following, once prop updates it will automatically rerender component:

render {

let textwhencomponentupdate = this.props.text 

return (
<view>
  <text>{textwhencomponentupdate}</text>
</view>
)

}

score:4

componentwillreceiveprops(nextprops) { // your code here}

i think that is the event you need. componentwillreceiveprops triggers whenever your component receive something through props. from there you can have your checking then do whatever you want to do.

score:4

i would recommend having a look at this answer of mine, and see if it is relevant to what you are doing. if i understand your real problem, it's that your just not using your async action correctly and updating the redux "store", which will automatically update your component with it's new props.

this section of your code:

componentdidmount() {
      if (this.props.ismanager) {
        this.props.dispatch(actions.fetchallsites())
      } else {
        const currentuserid = this.props.user.get('id')
        this.props.dispatch(actions.fetchuserssites(currentuserid))
      }  
    }

should not be triggering in a component, it should be handled after executing your first request.

have a look at this example from redux-thunk:

function makeasandwichwithsecretsauce(forperson) {

  // invert control!
  // return a function that accepts `dispatch` so we can dispatch later.
  // thunk middleware knows how to turn thunk async actions into actions.

  return function (dispatch) {
    return fetchsecretsauce().then(
      sauce => dispatch(makeasandwich(forperson, sauce)),
      error => dispatch(apologize('the sandwich shop', forperson, error))
    );
  };
}

you don't necessarily have to use redux-thunk, but it will help you reason about scenarios like this and write code to match.

score:22

you could use key unique key (combination of the data) that changes with props, and that component will be rerendered with updated props.

score:47

componentwillreceiveprops() is going to be deprecated in the future due to bugs and inconsistencies. an alternative solution for re-rendering a component on props change is to use componentdidupdate() and shouldcomponentupdate().

componentdidupdate() is called whenever the component updates and if shouldcomponentupdate() returns true (if shouldcomponentupdate() is not defined it returns true by default).

shouldcomponentupdate(nextprops){
    return nextprops.changedprop !== this.state.changedprop;
}

componentdidupdate(props){
    // desired operations: ex setting state
}

this same behavior can be accomplished using only the componentdidupdate() method by including the conditional statement inside of it.

componentdidupdate(prevprops){
    if(prevprops.changedprop !== this.props.changedprop){
        this.setstate({          
            changedprop: this.props.changedprop
        });
    }
}

if one attempts to set the state without a conditional or without defining shouldcomponentupdate() the component will infinitely re-render


Related Query

More Query from same tag