Sat*_*yam 6 react-native redux-thunk redux-toolkit
我在将extraReducer添加到 createSlice时收到上述错误。
这是一个反应本机应用程序
这是我的代码:
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;
},
},
});
Run Code Online (Sandbox Code Playgroud)
这是我从另一个文件中获取的调度代码:
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);
Run Code Online (Sandbox Code Playgroud)
为了确认调度,我确实编写了另一个具有相同名称的函数 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))
};
Run Code Online (Sandbox Code Playgroud)
通过上面提到的函数,我确实调用了 API 并检查是否成功。效果很好。我必须编写一个减速器来loginSucess 交叉检查 API 是否正常工作。它确实工作正常
我不明白我错在哪里!
需要帮忙!!
这是错误的屏幕截图:
| 归档时间: |
|
| 查看次数: |
3916 次 |
| 最近记录: |