我正在学习react-native
导航https://reactnavigation.org/docs/intro/。我在例子中看到了
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<View>
<Text>Hello, Chat App!</Text>
<Button
onPress={() => navigate('Chat')}
title="Chat with Lucy"
/>
</View>
);
}
}
我无法理解const { navigate } = this.props.navigation;
答案 0 :(得分:10)
语法与React Native无关 它在es6 / es2015中被称为Destructuring assignment
const { navigate } = this.props.navigation;
与var和const的异常相同。
var navigate = this.props.navigation.navigate
没有Destructuring的示例应该如下所示
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
return (
<View>
<Text>Hello, Chat App!</Text>
<Button
onPress={() => this.props.navigation.navigate('Chat')}
title="Chat with Lucy"
/>
</View>
);
}
}
答案 1 :(得分:0)
在this.props.navigation
上包含以下内容:
<HomeScreen navigation={this.props.navigation}/>
因为props.navigation
默认是在父组件上
,在HomeScreen组件上,您将访问导航,例如:
..
goToSignUp() {
this.props.navigation.navigate('SignUp');
}
..
对我而言,之前也感到困惑。干杯!