无法读取未定义的属性“ navigate”-React Native

时间:2018-09-14 06:22:28

标签: react-native navigation

我是React-Native的新手。我只是不断收到与“无法读取未定义的属性'navigate'”相同的错误。这是示例代码

export default class Form extends Component {
    constructor(props) {
        super(props);
        this.loginHandler = this.loginHandler.bind(this);
        this.navigate  = this.props.navigation;
        this.successCallback = this.successCallback.bind(this);
        this.state = {
            email: '',
            password: '',
        }
    }

    // TODO - Login Authentication
    loginHandler() {
        var loginUrl = 'someURL';
        fetch(loginUrl, {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                username: this.state.email,
                password: this.state.password
            }),
        })
            .then(response => { return response.json(); })
            .then(responseData => { return responseData; })
            .then((responseData) => {
                this.successCallback(responseData.token)
            })
            .catch((err) => { console.log(err); });

    }

    successCallback(token) {
        if (token === undefined)
            alert("Email or password is incorrect !");
        else {
            this.props.navigation.navigate('User', {tokenFromUser: token});
        }

}

在successCallback函数中,如果生成令牌,则表示登录凭据正确,因此应使用参数“令牌”导航到UserScreen。谁能帮我 ?

这是我如何调用loginHandler函数

        <TouchableOpacity style={styles.button} onPress={this.loginHandler} >
            <Image source={loginImage} style={styles.image} />
            <View style={styles.SeparatorLine} />
            <Text style={styles.text}>
                Enter
            </Text>
        </TouchableOpacity>

1 个答案:

答案 0 :(得分:2)

好吧,我找到了解决方案。在我称之为Form.js组件的地方,我只是添加了{... this.props}作为道具。

<Form {...this.props}   />

之所以这样,是因为Form.js的道具为空。