javascript - 使用 React Navigation 在 axios 请求后无法导航

标签 javascript react-native

我有一个问题。登录后我无法导航到 HomeScreen。 有一个错误:

Undhandled Promises Rejection(id:0): TypeError: undefined is not an object (evaluating '_this.props.navigation.navigate').

这是我的代码:

    _submitLogin = () => {
        this.setState({ isLoading: true });

        if(!this.state.nim || !this.state.password) {
            this._handleError('Please enter your NIM and Password');
        } else {
                axios({
                method: 'post',
                url: 'http://192.168.43.114/api.connectUBK/auth',
                data: {
                    nim: this.state.nim,
                    password: this.state.password
                }
            }).then((response) => {
                 if(response.data.status === 'Failed'){
                    this._handleError(response.data.message);
                  } else {
                    this._makeSession(response.data.user); 
                  }
            }).catch((error) => {
                this._handleError('Failed to access ConnectUBK');
            });

        }
    };

    _makeSession = async (session) => {
        const user = JSON.stringify(session);
        await AsyncStorage.setItem('user', user);
        this.props.navigation.navigate('AuthLoading');
    }

    _handleError = (message) => {
        this.setState({ isLoading: false });
        ToastAndroid.showWithGravity(
                message,
                ToastAndroid.SHORT,
                ToastAndroid.CENTER
        );
    };

    _renderButton = () => {

        if(this.state.isLoading) {
            return <ActivityIndicator color='white' size='large' />;
        }

        return (
            <TouchableOpacity style={styles.buttonContainer} onPress={this._submitLogin}>
                <Text style={styles.buttonText}>Login</Text>
            </TouchableOpacity>
        );
    };

请帮助我。 我卡住了

最佳答案

您是否将 this.props.navigation.navigate 作为 Prop 传递给您向我们展示的组件?似乎您必须添加它,所以它失败了。您需要将 navigation.navigate 添加为组件的 Prop 。

关于javascript - 使用 React Navigation 在 axios 请求后无法导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52686393/

相关文章:

javascript - 使用 AJAX 的动态级联下拉列表

javascript - 是否可以在 html 文本字段中显示图像?

javascript - 忽略掉毛和格式化的行 - VSCODE EsLint + Prettier

android - null 不是对象(评估 'RCTToastAndroid.SHORT' )

javascript - 如何为父屏幕包裹的子组件实现 React 导航器?

javascript - 在react router v4中渲染子路由

javascript - 如何将事件 CSS block 导航元素(跨度)设置为与使用 Javascript 和 "active"类不同的颜色?

javascript - Mozilla 插件中的模块和动态导入

android - 从 .apk 到 .aab 的本地构建版本 react ,如何将应用程序发送给客户端?

react-native - 要在Visual Studio Code中为React Native自动完成工作,需要采取什么步骤?