Reactjs 动作必须是普通对象。Redux不工作
我正在尝试添加redux thunk中间件,以便在我的应用程序中提供注册和登录功能。但是这个代码似乎对register不起作用。它给出了以下错误- 动作必须是普通对象。使用自定义中间件进行异步操作 代码如下 Index.js-导入thunk并使用它Reactjs 动作必须是普通对象。Redux不工作,reactjs,react-redux,redux-thunk,Reactjs,React Redux,Redux Thunk,我正在尝试添加redux thunk中间件,以便在我的应用程序中提供注册和登录功能。但是这个代码似乎对register不起作用。它给出了以下错误- 动作必须是普通对象。使用自定义中间件进行异步操作 代码如下 Index.js-导入thunk并使用它 import {createStore, applyMiddleware} from 'redux'; import thunk from 'redux-thunk'; const store = createStore(authReducer
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
const store = createStore(authReducer,applyMiddleware(
thunk,
loggerMiddleware
),window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'));
components/Register.js-使用redux使用上述注册函数的组件
import * as actions from '../actions/authActions';
class RegisterForm extends React.Component{
handleRegister = (e)=>{
e.preventDefault();
console.log("inside handle register");
console.log(this.props);
this.props.register();
}
}
var Register = connect(mapStateToProps,actions)(RegisterForm);
authReducer.js
/action/types.js
您的商店设置似乎不正确。将您的店铺写为:
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
authReducer,
{},
composeEnhancers(
applyMiddleware(
thunk,
loggerMiddleware
)
)
);
您可以在操作中尝试以下代码段吗
export function register=()=>{
return {
type:'auth_user'
}
}
您的店铺设置错误。请告诉我是怎么回事。以及如何纠正。嗨,阿鲁普,这是消除错误,但动作不会进入减速器。你能帮我一下吗?@AbhishekKulshrestha那个动作在做异步调用吗?如果没有,那么就不需要执行return dispatch=>{。将来它将执行异步调用,但现在我只是检查这段代码。如果这样做有效,我将把异步代码放在这段代码中。当它是异步调用时,只需这样编写,直到像非异步操作创建者那样编写操作。。
export const AUTH_USER = 'auth_user'
export const UNAUTH_USER = 'unauth_user'
export const AUTH_ERROR = 'auth_error'
export const FETCH_MESSAGE = 'fetch_message'
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
authReducer,
{},
composeEnhancers(
applyMiddleware(
thunk,
loggerMiddleware
)
)
);
export function register=()=>{
return {
type:'auth_user'
}
}