私のツールバーはすでにレンダリングされていますので、私はそのツールバーの状態を更新する必要があるときにcomponentWillUpdate()を呼び出す必要がありますか?子コンポーネントの状態を更新するためにcomponentWillUpdate()を呼び出す方法は?
のホームスクリーン
constructor(){
super();
this.state = {
pagenum: 0,
toptoolbartitle: "default",
homeviewtitle: "default",
};
}
updateHomeViewTitle(viewtitle){
console.log(viewtitle);
this.state.homeviewtitle = viewtitle;
console.log("homeviewtitle is now"+this.state.homeviewtitle);
}
render() {
return (
<View style={styles.container}>
<View style={{flex:1}}>
<TopToolbarAndroid
defaulttitle={"default"} titleupdate={this.state.homeviewtitle}/>
<RNCamera updateviewtitle={this.updateHomeViewTitle.bind(this)} />
<BottomBar />
</View>
</View>
はcomponentWillUpdateが呼び出されていない(再レンダリング)、ツールバーのタイトルを更新していない
constructor(props){
super();
this.props = props;
this.state = {
pagenum: 0,
title: this.props.defaulttitle
};
}
componentWillUpdate(){
console.log("component will update?");
if (this.props.titleupdate != null)
{console.log("updating toolbar title to: "+this.props.titleupdate);
this.setState({title: this.props.titleupdate});}
}
render() {
return (
<ToolbarAndroid
title= {this.state.title}
style={styles.toolbar}
/>
);
}
結果 をTopToolbarAndroid。
私はコールバックがhomeviewtitleを更新した後、それは無限ループを引き起こし()forceUpdateを使用してみてください。どの仕様の部分がわからないのですか?https://facebook.github.io/react/docs/component-specs.html 私は誤解しています。 – jsky
componentWillUpdateの代わりにcomponentWillMountも試しました – jsky