1
私はNavbarに渡すNavBarRouteMapperオブジェクトを持っています。しかし、ボタンの1つのオンプレス機能では、状態にアクセスする必要がありますが、非機能なので、オブジェクトに「これ」をバインドする方法がわかりません。関連するコードバインド 'this' to 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>クラスで起こってonclickのではなく、その私は私の知っているどのような方法でNavigationBarRouteMapperに状態を渡すことができません。 – omriki
どのような種類のパターンを使用しているのか分かりませんが、一番外側のコンポーネントは状態を持つ必要があります。フラックスパターンを使用している場合は、状態変更をトリガーするアクションを持つ必要がありますので、onclickは状態を変更するストアまたはディスパッチメソッドを起動します。その時点でフラックスに100%慣れているわけではありません。 –
最も外側のコンポーネントは状態を持っています。しかし、NavigationBarRouteMapperの中で 'this'を呼び出すと、最も外側のコンポーネントの代わりにその状態が取得されます。一番外側のオブジェクトをRouteMapperにバインドすることはできないので、どのように私はそれに反作用コンポーネントではないので、私はそれに小道具を渡すだろうか分からない。 – omriki