Abd*_*bdo 26 reactjs react-native
我知道已经存在类似的问题,但那里没有共享代码.
在navbarChanged()>如果条件下,我正在做this.setState.这是类型,HomeTab但setState似乎没有工作.
任何线索/指针?
class HomeTab extends React.Component {
constructor() {
super()
this.setState({
isNavBarHidden: false
});
}
updatePosition(lastPosition) {
}
navbarChanged() {
console.log("received navbar changed event", AppStore.navbarVisible());
if (AppStore.navbarVisible()) {
StatusBarIOS.setHidden(false)
this.setState({ isNavBarHidden: false})
// this.state.isNavbarHidden is still true here
this.render();
}
else {
StatusBarIOS.setHidden(true);
this.setState({ isNavBarHidden: true});
this.render();
}
}
componentDidMount() {
AppStore.addNavbarChangeListener( this.navbarChanged.bind(this) );
}
componentWillMount() {
StatusBarIOS.setHidden(false)
this.setState({ isNavBarHidden: false });
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的render()代码:
render() {
return (
<NavigatorIOS style={styles.container}
navigationBarHidden={this.state.isNavBarHidden}
ref="navigator"
initialRoute={{
title: 'Foo',
component: HomeScreen,
passProps: { parent: this }
}}
/>
)
}
Run Code Online (Sandbox Code Playgroud)
Col*_*say 60
不要明确打电话render.当状态或道具发生变化时,React会自动重新渲染,因此不需要这样做.另外,你的实际render方法在哪里?
至于你的问题,setState是异步的,所以试图在setState调用后直接使用状态不起作用,因为更新不一定会运行.相反,您可以使用第二个参数setState作为回调:
this.setState({ myVal: 'newVal'}, function() {
// do something with new state
});
Run Code Online (Sandbox Code Playgroud)
在设置状态之后和重新呈现组件之后将触发此操作.
使用 state 作为 es6 类的实例成员,而不是 setState。稍后可以调用 setState 函数以确保必要的重新渲染。
constructor() {
super()
this.state = {
isNavBarHidden: false
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36122 次 |
| 最近记录: |