componentWillReceiveProps wird nicht ausgelöst
In meinen anderen Klassen hat componentWillReceiveProps gut funktioniert, aber aus irgendeinem Grund wird es hier nicht ausgelöst.
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});
}
...
}
Die an itemView übergebene Requisite ist entweder null, wenn nichts übergeben werden soll, oder das Array, das ItemView ihr zuweist.
componentWillReceiveProps () wird nur ausgelöst, wenn die Attribute des Status null werden, nicht jedoch, wenn sie festgelegt werden. ((null -> eintrag) funktioniert nicht, aber (eintrag -> null) funktioniert).
Bin ich etwas verpasst? Vielen Dank
- bearbeiten:
(null -> entry) aktualisiert den Status, ruft jedoch weder die Protokolle noch nachfolgende componentWillRecieveProps () auf. (Aber der Eintrag -> null tut es.)