【问题标题】:Reducer use axios to send a request return reponse object failedReducer 使用 axios 发送请求返回响应对象失败
【发布时间】:2017-06-10 12:14:42
【问题描述】:

const requestUserInfo = (state = {}, action) => {
	switch(action.type) {
		case 'HEADER_GET_USERINFO':
			axios.post('/user/getScores', qs.stringify({
				token: token,
				uid: uid
			}))
			.then(res => {
				if(res.data.status !== 200) {
					message.error(res.data.message)
					return state
				}

				return { ...res.data.attachment, ...state }
			})
			break
		default :
			return state
	}
}
// error
Uncaught Error: Given action "HEADER_GET_USERINFO", reducer "requestUserInfo" returned undefined. To ignore an action, you must explicitly return the previous state.

【问题讨论】:

    标签: reactjs react-redux axios


    【解决方案1】:

    你应该避免在你的 reducer 中产生副作用!

    正如官方 Redux 教程中提到的:

    你不应该在 reducer 中做的事情:

    1. 改变它的参数
    2. 执行 API 调用和路由转换等副作用
    3. 调用非纯函数,例如Date.now() 或 Math.random()。

    您应该改用Async actions,它将执行 API 调用,并且至少可能调度一个应该处理传入数据的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-21
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2021-02-03
      • 1970-01-01
      • 2023-01-21
      相关资源
      最近更新 更多