1
我有一個NavBarRouteMapper對象,我傳遞給我的導航欄。然而,在我需要訪問狀態的按鈕的onpress函數中,但我不確定如何將「this」綁定到對象,因爲它是非函數。相關代碼如下將'this'綁定到NavigationBarRouteMapper無功能
class app extends Component {
state: {
sideMenuIsOpen: boolean,
};
constructor(props: Object) {
super(props);
this.state = {
sideMenuIsOpen: false,
};
};
static NavigationBarRouteMapper = {
LeftButton(route, navigator, index, navState) {
if (index > 0) {
return (
<SimpleButton
// TODO: Make this work, Menu button needs to go to this
// The problem is here. this.state is undefined
onPress={console.log(this.state)}
customText="Back"
style={styles.navBarLeftButton}
textStyle={styles.navBarButtonText}
/>
);
}
},
RightButton(route, navigator, index, navState) {
// TODO change add button for admins
switch (route.title) {
case "Login":
return null;
default:
return (
<SimpleButton
onPress={() => {
navigator.push({
title: "Profile",
component: Profile,
});
}}
customText="Add"
style={styles.navBarRightButton}
textStyle={styles.navBarButtonText}
/>
);
}
},
Title(route, navigator, index, navState) {
return (
<Text style={styles.navBarTitleText}>{route.title}</Text>
);
},
};
render() {
return (
<SideMenu
menu={<Menu navigate={this.navigate} />}
isOpen={this.state.sideMenuIsOpen}
>
<Navigator
ref="rootNavigator"
initialRoute={{
title: "Login",
component: LoginScene,
navigator: this.refs.rootNavigator,
}}
renderScene = {this.renderScene}
navigationBar={
<Navigator.NavigationBar
// Since this is an object, I can't bind 'this' to it,
// and the documentation calls for it to be passed an object
routeMapper={app.NavigationBarRouteMapper}
style={styles.navBar}
/>
}
/>
</SideMenu>
);
};
}
的問題是不與<使用SimpleButton>類,它是我不能將狀態傳遞到NavigationBarRouteMapper以任何方式,我知道的onclick發生。 – omriki
我不確定你使用的是什麼樣的模式,你最外面的組件應該有狀態。如果你使用的是流量模式,你應該有觸發狀態變化的動作,這樣你的onclick會把調度方法激發到一個商店,這個商店會改變狀態,或者是出現這種情況,但我現在還不是100%熟悉流量。 –
最外面的組件確實有一個狀態。然而,當在NavigationBarRouteMapper中調用'this'時,它將獲得狀態而不是最外面的組件。由於我無法將最外層的對象綁定到RouteMapper,因此我不確定如何將道具傳遞給它,因爲它不是React組件。 – omriki