简体   繁体   English

类型错误:未定义不是对象(评估“_this2.props.navigation.navigate”)

[英]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.

相关问题 未定义不是对象(评估_this2.props.navigation.navigate) - undefined is not an object (evaluating _this2.props.navigation.navigate) 未定义的导航问题不是 object(正在评估“_this2.props.navigation.navigate”) - Navigation issue undefined is not an object (evaluating '_this2.props.navigation.navigate') 反应本机错误“未定义不是对象(正在评估&#39;_this2.props.navigation.navigate&#39;)” - React Native Error “undefined is not an object (evaluating '_this2.props.navigation.navigate')” undefined不是递归组件上的对象(评估&#39;_this2.props.navigation.navigate&#39;) - undefined is not an object (evaluating '_this2.props.navigation.navigate') on a recursive component undefined 不是一个对象(评估&#39;_this2.props.navigation.navigate&#39;)-React Native - undefined is not an object (evaluating '_this2.props.navigation.navigate') - React Native React Native:undefined不是对象(评估&#39;_this2.props.navigation.navigate&#39;) - React Native : undefined is not an object (evaluating'_this2.props.navigation.navigate') undefined不是对象(评估&#39;_this2.props.navigation.navigate&#39;)onPress React-Native - undefined is not an object (evaluating '_this2.props.navigation.navigate') onPress React-Native 类型错误:未定义不是对象(评估“_this.props.navigation.navigate”) - TypeError: undefined is not an object (evaluating '_this.props.navigation.navigate') TypeError: undefined is not an object (evalating '_this.props.navigation.navigate') reactNavigation 5 - TypeError: undefined is not an object (evaluating '_this.props.navigation.navigate') reactNavigation 5 未定义不是对象(评估this.props.navigation.navigate) - undefined is not an object (evaluating this.props.navigation.navigate)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM