javascript - 动作必须是普通对象。使用自定义中间件进行异步操作。React、Redux、Thunk
问题描述
请帮忙,这在过去的四天里一直困扰着我,我无法解决。
我正在使用 react 和 redux 来提取一些数据,这些数据来自我知道我已经安装了 thunk 但我不知道如何使用它。但是我的动作又回来了:动作必须是普通对象。使用自定义中间件进行异步操作。
行动:
export const getShifts = () => dispatch => {
console.log('Fetching list of shifts for user...');
const request = API.get("StaffAPI", "/shifts", {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then(response =>
dispatch({type: 'SHIFTS_LOAD_SUCCESS', response})
)
.catch(err =>
dispatch({type: 'SHIFTS_LOAD_FAIL'})
)
}
减速器:
import _ from "lodash"
import { getShifts} from '../actions';
export default function(state = {}, action) {
switch(action.type){
case SHIFTS_LOAD_SUCCESS:
return { ...state, rota: action.response };
}
}
零件:
componentWillMount() {
this.props.getShifts();
}
renderPosts(){
console.log(this.props);
return (
<div>
<button onClick={this.logOut}>LogOut</button>
<h4>hello{this.props.rotaData}</h4>
</div>
);
}
render() {
return (
<div className="row">
{this.renderPosts()}
</div>
);
}
}
function mapStateToProps(state){
return{ rota: state.response };
}
export default connect(mapStateToProps, {getShifts: getShifts}) (StaffRota);
我对这一切都很陌生,如果它很简单,请原谅我。Know-one else 已经能够解决我的问题。
解决方案
您redux-thunk
在创建商店时进行了设置吗?它应该是这样的
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
// Create store
const store = createStore(yourReducers, applyMiddleware(thunk));
尝试仔细检查 thunk 文档https://github.com/reduxjs/redux-thunk
推荐阅读
- lsyncd - 当目标主机不可用时,LSYNC 不起作用
- react-native - 反应原生初始化应用程序有问题
- c# - 有没有办法将运行时可视化树转换回 XAML 代码/文本?
- ruby - Rails 5.2 ActionMailer 邮件不会发送。在服务器中看不到任何错误
- javascript - 如何使用 React-Intl 呈现格式化消息列表
- kubernetes - 如何让 Kubernetes api 服务器实例通过代理连接到外部网络
- pandas - Pandas 按两列分组并根据其中一列中的值(分类)将数据写入特定列
- django - 无法查询“ABC”:必须是“Image”实例
- symfony - Symfony 2 - 在一个实体上处理多个实体侦听器
- javascript - javascript更改背景并一键播放声音