调度导航动作

时间:2020-03-16 03:45:24

标签: react-native react-navigation

这是我的代码

import { NavigationActions } from "react-navigation"
import { call, put, delay } from "redux-saga/effects"
import { AsyncStorage, Alert } from "react-native"
export function* handleResponse(response) {
    switch (response.status) {
        case 200: {
            console.log('logged by phuognn aaa');
            Alert.alert("404400404040400")
            yield call(NavigationActions.navigate, { routeName: "Auth" })
            return true;
        }
        case 201: {
            console.log('logged by phuognn bbb');
            return false;
        }
    }
}

日志行和警报已运行,我想导航到路由“ Auth”,但不起作用。 我的代码有问题吗?

2 个答案:

答案 0 :(得分:0)

差错

yield call(NavigationActions.navigate, { routeName: "Auth" })

替换为

this.props.navigation.navigate("Auth");

这里是理解的链接

在屏幕之间移动https://reactnavigation.org/docs/navigating

答案 1 :(得分:0)

NavigationActions.navigate是动作创建者函数,而不是api调用或类似函数。要从传奇中分派动作,应使用put而不是call

import { NavigationActions } from "react-navigation"
import { call, put, delay } from "redux-saga/effects"
import { AsyncStorage, Alert } from "react-native"
export function* handleResponse(response) {
    switch (response.status) {
        case 200: {
            console.log('logged by phuognn aaa');
            Alert.alert("404400404040400");
            yield put(NavigationActions.navigate({ routeName: "Auth" }));
            return true;
        }
        case 201: {
            console.log('logged by phuognn bbb');
            return false;
        }
    }
}

请尝试使用此代码。我对redux-saga不太熟悉。我更喜欢redux-observable,所以请告诉我这是否可行。