gpt4 book ai didi

javascript - 在 Redux 中,reducer 无法识别 action 对象

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

我在 React 项目中使用 Redux。出于某种原因,我的 reducer 无法识别发送给它的操作 type 甚至操作本身。我收到此错误 TypeError: Cannot read property 'type' of undefined。我知道我正在使用 dispatch

来自服务器的 api 工作正常,我已经通过 Postman 进行了测试。

但我不明白 redux 发生了什么。

拜托,有人可以告诉我吗?

在解决我的问题之前,我已经阅读了许多看起来相似但没有人回答我的问题的 SO 帖子,因此我为什么要在这里问它。

谢谢。

Action :

import axios from 'axios';

export const GET_ALL_USERS = 'GET_ALL_USERS';

export const showAllUsers = () => dispatch => {
console.log('USERS ACTION');

return axios
.get('/api/users/all')
.then(res => {
console.log('GETTING ALL USERS ACTION', res);

return dispatch({
type: GET_ALL_USERS,
payload: res.data
});
})
.catch(err => console.log('Oops! Cannot get any users.'));
};

reducer :

import { GET_ALL_USERS } from '../actions/usersActions';

const InitialState = {
users: null,
loading: false
};

export default function(state = InitialState, action) {
console.log('USERS REDUCER', action);

switch (action.type) {
case GET_ALL_USERS:
return {
...state,
users: action.payload
};
default:
return state;
}
}

react :

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import './index.css';
import App from './containers/App';
import registerServiceWorker from './registerServiceWorker';

import rootReducer from './reducers';

let middleware = [thunk];

const store = createStore(
rootReducer,
{},
compose(
applyMiddleware(...middleware),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
);

ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
registerServiceWorker();

react 组件:

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';

import { showAllUsers } from '../../actions/usersActions';

export class Admin extends Component {
constructor(props) {
super(props);

this.state = {};
}

componentDidMount() {
this.props.showAllUsers();
}

render() {
const { users } = this.props;
console.log(`All users in admin`, this.props.users);

return (
<div className="admin">
<h1 className="admin__title">Admin Board</h1>
{this.props.users.map(user => {
return (
<article>
<img src={user.avatar} alt={user.username} />
<p>{user.firstName}</p>
<p>{user.lastName}</p>
<p>{user.username}</p>
<p>{user.email}</p>
</article>
);
})}
</div>
);
}
}

const mapStateToProps = state => ({
users: state.users
});

export default connect(mapStateToProps, { showAllUsers })(Admin);

最佳答案

showAllUsersasync action .您需要一些中间件来调度异步操作

使用redux-thunkredux-saga并将其与商店整合。这将有助于执行异步调度

import thunkMiddleware from 'redux-thunk';
let middleWares = [thunkMiddleware];


const store = createStore(
rootReducer, applyMiddleware(...middleWares)
)

如果您以同步方式调度异步操作,将抛出错误TypeError: Cannot read property type of undefined

关于javascript - 在 Redux 中,reducer 无法识别 action 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50859456/

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