gpt4 book ai didi

react-native - 类型错误 : Cannot read property 'pending' of undefined in the redux toolkit while using createAsyncThunk and createSlice (@reduxjs/toolkit": "^1. 4.0)

转载 作者:行者123 更新时间:2023-12-04 11:30:12 30 4
gpt4 key购买 nike

我在添加 时遇到上述错误extraReducer 到我的 创建切片。
这是一个 react 原生应用程序
这是我的代码:

    export const login = createAsyncThunk(
'loginAuth/login',
async ({username, password}) => {
try {
const res = await api.post('SomeApi', {
username,
password,
});

return res.data;
} catch (e) {
return console.error(e.message);
}
},
);

const loginSlice = createSlice({
name: 'loginAuth',
initialState: {
loginStatus: false,
isLoading: false,
error: '',
},
reducers: {
//Yet to be filled
},
extraReducers: {
[login.pending]: (state) => {
state.isLoading = true;
},
[login.fulfilled]: (state, action) => {
state.isLoading = false;
},
[login.rejected]: (state, action) => {
state.error = action;
},
},
});
这是我来自另一个文件的调度代码:
    class Login extends Component {
state = {
data: {
username: '',
password: '',
},
textHidden: true,
};

handelSubmit = (status) => {
if (status) {
this.props.login(this.state.data);
}
};
render(){
return(
//The UI for Input is here. I confirmed that the dispatch is working fine. I did log the username and password. But I didn't use the createAsyncThunk
)
}

const mapDispatchToProps = (dispatch) => ({
login: (data) => dispatch(login(data)),
});

export default connect(null, mapDispatchToProps)(Login);
为了确认调度,我确实编写了另一个具有相同名称 login() 的函数,我在其中记录了用户名和密码:
    export const login = ({username, password}) => async (dispatch) => {
console.log(username,password); // Here the dispatch is working fine
// called that API and dispatched to a reducer dispatch(loginSucess(result.data))
};
使用上面提到的功能,我确实调用了 API 并检查了是否成功。它工作得很好。我不得不为 loginSucess 写一个 reducer 交叉检查 API 是否正常工作。它确实工作正常
我不明白我哪里出错了!!
需要帮忙!!
这是错误的屏幕截图:
enter image description here

最佳答案

我得到了答案。你查的一样here .这是我的愚蠢。我没有正确安排代码。 login应该在loginSlice之上.谢谢!!

关于react-native - 类型错误 : Cannot read property 'pending' of undefined in the redux toolkit while using createAsyncThunk and createSlice (@reduxjs/toolkit": "^1. 4.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65194394/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com