gpt4 book ai didi

reactjs - react native Redux useDispatch 不工作

转载 作者:行者123 更新时间:2023-12-05 06:18:00 24 4
gpt4 key购买 nike

我尝试创建一个 Redux,但当我尝试进行分派(dispatch)时遇到问题,但无法正常工作。

Action 文件,userActions.js:

export const setId = () => {
console.log("Enter to set id func");
return {
type: 'SET_ID'
}
}

Reducer 文件,userReducer.js:

const INITIAL_STATE = {
id: "",
email: "",
name: "",
};

const userReducer = (state = INITIAL_STATE, action) => {
console.log('Enter to userReducer');
switch (action.type) {
case "SET_ID": {
// console.log(action.payload);
}
default: {
return state;
}
}
}

export default userReducer;

合并 reducer 文件:

import userReducer from "./userReducer";
import { combineReducers } from "redux";

const allReducers = combineReducers({
userReducer: userReducer
})

export default allReducers;

App.js 文件:

import React from 'react';
import Routes from "./Routes";
import { createStore } from "redux";
import allReducer from "./app/reducers";
import { Provider } from "react-redux";

const store = createStore(
allReducer
);

const App = () => {
return (
<Provider store={store}>
<Routes />
</Provider>
);
};

export default App;

在登录屏幕文件中,当我点击他调用时我有一个按钮来发送到“setId”操作。
这是我来自 Login.js 的一些代码:

import { useDispatch } from 'react-redux';
import { setId } from '../actions/userActions';
handleLoginResult = (error, user) => {
console.log('Enter to handleLoginResult');
if (error !== "") {
this.setState({ generalError: error });
} else {
const dispatch = useDispatch();
console.log('uid: ', user.user.uid);
dispatch(setId());
alert("Login!");
}
}

有什么问题,为什么不进入 setId 操作?

最佳答案

你可以这样试试

const userReducer = (state = INITIAL_STATE, action) =>dispatch =>{
console.log('Enter to userReducer');
switch (action.type) {
case "SET_ID": {
// console.log(action.payload);
}
default: {
return state;
}
}

关于reactjs - react native Redux useDispatch 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61427750/

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