【问题标题】:Action get dispatched but nothing happen动作被派发但什么也没发生
【发布时间】:2021-07-12 04:47:57
【问题描述】:

我正在尝试从动作负载中获取数据以在 redux-saga 中使用,但它返回 undefined

这是一些代码

动作

export const GetUserInfo = (user, password) => {
    return{
        type: actionList.GET_USER_INFO,
        data: {user, password},
    }
};

传奇部分

登录传奇

function* loginSaga(action) {

    console.log('Saga is working')
    const getJson = yield call(requestGetUser)
    const getJsonData = getJson

    const getJsonUsername = String(getJsonData.username)
    console.log(getJson)
    console.log('getjson data ', getJsonUsername)
    console.log(action.data.username) // try to get data but return undefined

    const getJsonPassword = String(getJsonData.password)

    if (String(action.data.username) === getJsonUsername) {
        if (String(action.data.password) === getJsonPassword) {
            console.log('saga login success')
            yield put({type: 'LOGIN_SUCCESS'})
            SaveToAsyncStorage(action.data)
        }
        else {
            console.log('saga password fail')
        }
    }
    else {
        console.log("saga user fail")
    }
}
export {loginSaga}

WatchSaga

export function* watchSaga() {
    yield takeLatest(GET_USER_INFO,loginSaga);
}

主文件使用useDispatch(username,password)来存储这些数据,它以redux扩展名显示,但返回未定义 这是一个 gif 来展示正在发生的事情

非常感谢

【问题讨论】:

标签: reactjs react-native redux redux-saga


【解决方案1】:

我仔细查看了您提供的 repo 有一个错误,您必须进行如下更改;

替换以下代码:

export const GetUserInfo = (user, password) => {
    return{
        type: actionList.GET_USER_INFO,
        data: {user, password},
    }
};

使用以下代码

export const GetUserInfo = (username, password) => {
    return{
        type: actionList.GET_USER_INFO,
        data: {username, password},
    }
}; 

原因是您试图从操作中获取用户名属性,但返回属性是用户。

【讨论】:

  • 天哪,它工作!!!!我真的很感谢你,多么愚蠢的错误,我逐行搜索代码,在stackoverflow上问了几十个问题但找不到解决方案,非常感谢先生,祝你有美好的一天
猜你喜欢
  • 2019-09-20
  • 1970-01-01
  • 2012-09-10
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
相关资源
最近更新 更多