私の他のクラスでは、componentWillReceivePropsはうまく機能していましたが、何らかの理由でここでは起動しません。componentWillReceiveProps not firing
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});
}
...
}
何も渡されていない、またはItemViewがそれに代入されているアレイ場合itemViewに渡される支柱のいずれかヌルです。
componentWillReceiveProps()は、状態の属性がnullになったときにのみ発生し、設定されていないときに発生します。 ((null - > entry)は動作しませんが、(entry - > null)は機能します)。
何かが欠落していますか?ありがとう!
- 編集:
(ヌル - >エントリ)の更新状態が、ログまたはそれ以降のcomponentWillRecievePropsを呼び出すことはありません()。 (ただし、エントリ - >ヌルはありません。)
'ItemView'の' componentWillReceiveProps'、または 'name'状態が正しく設定されているというクロムの' react'インスペクタプラグインを使って検証しましたか? 'name'は変更されていないので、' ItemView'はレンダリングされていないようです。 –
私はcomponentWillReceiveProps()関数とCommentBox.jsxのrender()関数の両方でログを記録しようとしました。不思議なことに、それはcomponentWilRecieveProps()内のログを出力しませんが、null - > entryの間にrender()内部の状態を変更します。入力を更新してchromeの出力を表示します。 –
"itemViewに渡された小道具は、何も渡されない場合、またはItemViewがそれに割り当てる配列です。 あなたの質問には間違いがありますか、または '' companyToRender''は配列ですか(あなたの言うとおり)?私はそれがオブジェクトでなければならないと思う。 また、componentWillReceivePropsは、コンポーネントが最初にマウントされたとき(ただしその後にのみ)、呼び出されないことに注意してください。 –