如何在 const 中使用反应导航?
How to use react navigation in a const?
我已经使用 const 来显示组件。现在,当我在 const 中对按钮使用反应导航时,我看到此错误:undefined is not an object (evaluating '_this.props.navigation.navigate')
我尝试将 navigation={this.props.navigation} 添加到按钮以允许导航,但没有成功。
const WomenTab = () => (
<View>
<Button onPress={() => {
this.props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Wallet' })
],
}))
}}>
<Text>Click</Text>
</Button>
<View>
);
图书馆link:http://github.com/react-native-community/react-native-tab-view
您需要将 props
传递给 const
,类似这样
const WomenTab = (props) => (
<View>
<Button onPress={() => {
props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Wallet' })
],
}))
}}>
<Text>Click</Text>
</Button>
<View>
);
然后当你使用你的 const
时,你传递你想要的道具。
<WomenTab navigation={this.props.navigation} />
这称为 functional component,通常称为无状态功能组件。
主要区别之一是 SFC 不会自动接收道具,而是必须作为参数传递。所以不要说 this.props
你应该使用这个模式:
const WomenTab = (props) => ( // <-- add props as an argument
<View>
<Button onPress={() => {
props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [ NavigationActions.navigate({ routeName: 'Wallet' }) ],
}))
}}>
<Text>Click</Text>
</Button>
<View>
);
由于导航道具会自动传递给导航器的子级,因此您无需执行任何其他操作。如果你想通过其他道具,你会像往常一样这样做:
<WomenTab myProp={value} />
另一个常见的模式是 destructure 像这样传递给 SFC 的道具:
const WomenTab = ( {navigation} ) => ( // <-- you can pick the props you want via destructuring
<View>
<Button onPress={() => {
navigation.dispatch(StackActions.reset({
index: 0,
actions: [ NavigationActions.navigate({ routeName: 'Wallet' }) ],
}))
}}>
<Text>Click</Text>
</Button>
<View>
);
希望对您有所帮助,祝您好运!
基本上你的道具不会从父组件传递到子组件。确保您已在 createStackNavigator 函数中定义了 WomenTab 组件。还要在你的功能组件中传递道具。
const WomenTab = (props) => (
<View>
<Button onPress={() => {
this.props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Wallet' })
],
}))
}}>
<Text>Click</Text>
</Button>
<View>
);
我已经使用 const 来显示组件。现在,当我在 const 中对按钮使用反应导航时,我看到此错误:undefined is not an object (evaluating '_this.props.navigation.navigate')
我尝试将 navigation={this.props.navigation} 添加到按钮以允许导航,但没有成功。
const WomenTab = () => (
<View>
<Button onPress={() => {
this.props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Wallet' })
],
}))
}}>
<Text>Click</Text>
</Button>
<View>
);
图书馆link:http://github.com/react-native-community/react-native-tab-view
您需要将 props
传递给 const
,类似这样
const WomenTab = (props) => (
<View>
<Button onPress={() => {
props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Wallet' })
],
}))
}}>
<Text>Click</Text>
</Button>
<View>
);
然后当你使用你的 const
时,你传递你想要的道具。
<WomenTab navigation={this.props.navigation} />
这称为 functional component,通常称为无状态功能组件。
主要区别之一是 SFC 不会自动接收道具,而是必须作为参数传递。所以不要说 this.props
你应该使用这个模式:
const WomenTab = (props) => ( // <-- add props as an argument
<View>
<Button onPress={() => {
props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [ NavigationActions.navigate({ routeName: 'Wallet' }) ],
}))
}}>
<Text>Click</Text>
</Button>
<View>
);
由于导航道具会自动传递给导航器的子级,因此您无需执行任何其他操作。如果你想通过其他道具,你会像往常一样这样做:
<WomenTab myProp={value} />
另一个常见的模式是 destructure 像这样传递给 SFC 的道具:
const WomenTab = ( {navigation} ) => ( // <-- you can pick the props you want via destructuring
<View>
<Button onPress={() => {
navigation.dispatch(StackActions.reset({
index: 0,
actions: [ NavigationActions.navigate({ routeName: 'Wallet' }) ],
}))
}}>
<Text>Click</Text>
</Button>
<View>
);
希望对您有所帮助,祝您好运!
基本上你的道具不会从父组件传递到子组件。确保您已在 createStackNavigator 函数中定义了 WomenTab 组件。还要在你的功能组件中传递道具。
const WomenTab = (props) => (
<View>
<Button onPress={() => {
this.props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Wallet' })
],
}))
}}>
<Text>Click</Text>
</Button>
<View>
);