Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

componentWillReceiveProps not firing

Tags:

In my other classes, componentWillReceiveProps was working nicely, but for some reason, it isn't firing here.

ItemView.jsx

class ItemView extends React.Component {     constructor(props) {         super(props);          this.state = {             name: null,              rating: null,              sector: null,              location: null,              description: null,              image: "blank.png",             show: false        };     }      ...      componentWillReceiveProps(nextProps) {          if(!nextProps.companyToRender) {             this.setState({                 name: null,                  rating: null,                  sector: null,                  location: null,                  description: null,                  image: "blank.png",                 show: false             });        }        else {            var companyToRender = nextProps.companyToRender;             this.setState({                name: companyToRender.name,                 rating: companyToRender.rating,                 sector: companyToRender.sector,                 location: companyToRender.location,                 description: companyToRender.description,                 image: companyToRender.image,                show: true            });     }      ...      render () {        return(         <div>          ...         <CommentBox show={this.state.show} companyToRender={this.state.name}/>          ...         </div>        );      } } 

CommentBox.jsx

class CommentBox extends React.Component {     constructor(props) {         super(props);         this.state = {companyToRender: null};     }      componentWillReceiveProps(nextProps) {         this.setState({companyToRender: nextProps.companyToRender});     }      ... } 

The prop passed to itemView is either null if nothing is to be passed, or the array that ItemView is assigning to it.

componentWillReceiveProps() fires only when the state's attributes become null, but not when it sets. ( (null --> entry) doesn't work but (entry --> null) works).

Am I missing out on something? Thanks!

-- edit:

(null --> entry) updates state, but doesn't call the logs or any subsequent componentWillRecieveProps(). (But entry --> null does.)

Logs for null --> entry

Logs for entry --> null

like image 275
Carlo Pascual Avatar asked Oct 30 '16 03:10

Carlo Pascual


People also ask

How do I trigger componentWillReceiveProps?

You need to find the first shared parent of the button and the component that you're trying to trigger componentWillReceiveProps for. SomeComponent is the first shared parent of AnotherComponent and the button .

What is the replacement for componentWillReceiveProps?

The useEffect hook is also the equivalent of the componentWillReceiveProps or componentDidUpdate hooks. All we have to do is to pass in an array with the value that we want to watch for changes.

How do I use componentWillReceiveProps in React?

ReactJS – componentWillReceiveProps() MethodThis method is used during the updating phase of the React lifecycle. This function is generally called if the props passed to the component change. It is used to update the state in response with the new received props.

Does Setstate trigger componentWillReceiveProps?

Yep, both are likely. React will try to render anytime it gets new props or state and because it does dom diffing, it tries to render as often as possible.


2 Answers

After much painful debugging, I found out that the problem was that ItemView was being called inside a modal (react-bootstrap) which for some reason, doesn't support componentWillReceiveProps(). Ended up fixing the issue by refactoring the code. Thanks guys!

like image 186
Carlo Pascual Avatar answered Sep 22 '22 06:09

Carlo Pascual


If anyone else is having a problem with this...

componentWillReceiveProps will not be called if you are receiving the exact same props as before. What you can do to get around it is add a dummy prop that iterates every time you want to send the same prop to the component in case that component internally resets itself somehow

click() {     this.setState({counter: ++this.state.counter, unchangingProp:true}) } <Component unchangingProp={this.state.unChangingProp} counter={this.state.counter}/> 

This way componentWillRecieveProps will be triggered every time.

like image 27
Leon Avatar answered Sep 21 '22 06:09

Leon