Javascript 在Redux中,reducer无法识别动作对象
我正在使用Redux进行React项目。出于某种原因,我的reducer无法识别发送给它的动作类型,甚至无法识别动作本身。我收到以下错误类型错误:无法读取未定义的属性“type”。我知道我在使用调度 来自服务器的api运行良好,我已经通过Postman进行了测试 但我不明白redux到底发生了什么 请问,有人能给我一些建议吗 在结束我的问题之前,我已经读了很多看起来相似的帖子,但是没有一篇回复我的问题,所以我在这里问这个问题 谢谢 行动: 减速器: 反应: 反应组分: showAllUsers是。您需要一些中间件来分派异步操作 使用或将其与应用商店集成。 它将有助于执行异步调度 如果以同步方式分派异步操作,将抛出错误TypeError:Cannot read属性类型为undefined。showAllUsers为。您需要一些中间件来分派异步操作 使用或将其与应用商店集成。 它将有助于执行异步调度Javascript 在Redux中,reducer无法识别动作对象,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我正在使用Redux进行React项目。出于某种原因,我的reducer无法识别发送给它的动作类型,甚至无法识别动作本身。我收到以下错误类型错误:无法读取未定义的属性“type”。我知道我在使用调度 来自服务器的api运行良好,我已经通过Postman进行了测试 但我不明白redux到底发生了什么 请问,有人能给我一些建议吗 在结束我的问题之前,我已经读了很多看起来相似的帖子,但是没有一篇回复我的问题,所以我在这里问这个问题 谢谢 行动: 减速器: 反应: 反应组分: showAllUsers是
如果以同步方式分派异步操作,将抛出错误TypeError:Cannot read属性类型为undefined。我忘了提到我在项目中也使用了redux thunk。我会更新代码。抱歉,商店里的redux thunk仍然不起作用。所以我一定是遗漏了什么。@Shaoz仍然是相同的错误吗?请您分享一下。在component add const{dispatch}=this.props;调度员;我忘了提到我在项目中也使用了redux thunk。我会更新代码。抱歉,商店里的redux thunk仍然不起作用。所以我一定是遗漏了什么。@Shaoz仍然是相同的错误吗?请您分享一下。在component add const{dispatch}=this.props;调度员;
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.'));
};
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;
}
}
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();
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);
import thunkMiddleware from 'redux-thunk';
let middleWares = [thunkMiddleware];
const store = createStore(
rootReducer, applyMiddleware(...middleWares)
)