[英]TypeError: undefined is not an object (evaluating '_this2.props.navigation.navigate')
I've already read react native navigation docs but couldn't find any solution我已经阅读了本机导航文档,但找不到任何解决方案
I want to send props using children, but I got this error when navigating (onPress)我想使用儿童发送道具,但在导航 (onPress) 时出现此错误
album.js相册.js
const StackAlbum = createStackNavigator()
class Album extends Component {
constructor(props) {
super(props)
this.state = {
albums: [],
photo: []
}
}
componentDidMount() {
// Get data from link then set state
fetch("https://jsonplaceholder.typicode.com/albums")
.then(res => res.json())
.then(jsonRes => this.setState({
albums: jsonRes
}))
.catch(err => console.log(err))
}
render() {
return (
<StackAlbum.Navigator
initialRouteName="AlbumScreen"
>
<StackAlbum.Screen
name="AlbumScreen"
children={() => <AlbumScreen
albums={this.state.albums} //HERE WE GO
/>}
/>
<StackAlbum.Screen
name="PhotoScreen"
component={PhotoScreen}
options={{
headerShown: true
}}
/>
</StackAlbum.Navigator>
)
}
}
export default Album
albumScreen.js相册屏幕.js
render() {
return (
<ScrollView>
<Text>Albums</Text>
<TouchableOpacity
style={styles.button}
onPress={() => this.props.navigation.navigate("PhotoScreen")} // HERE IS THE ERROR
>
<Text>Go To Photo</Text>
</TouchableOpacity>
<View>
{this.showAlbum()}
</View>
</ScrollView>
)
}
I've tried using component={AlbumScreen} but confused to send props.我试过使用 component={AlbumScreen} 但对发送道具感到困惑。 anyone has solution?
有人有解决方案吗?
Here's how to pass props to a screen from navigator:以下是如何将道具从导航器传递到屏幕:
<StackAlbum.Screen name="AlbumScreen">
{props => <AlbumScreen{...props} albums={this.state.albums} />}
</StackAlbum.Screen>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.