采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
老师,请帮我看一下这个报错的原因: The previous state received by the reducer is of unexpected type. Expected argument to be an instance of Immutable.Collection or Immutable.Record with the following properties: “header”.
list: fromJS(action.data)
不明白... (1)在changeList这个actionCreator中,专门在data属性上调用了fromJS; (2)在getLIst方法中把changeLIst创建的action传递给store。 这样reducer接收到的action的data属性也是immutable类型的吧?如果是,在reducer中为何要对再对data调用fromJS?
//src/common/header/store/reducer import * as constants from './constants' import {fromJS} from 'immutable' const defaultState=fromJS({ focused: false, list: [] }) export default (state=defaultState, action)=>{ switch(action.type){ case constants.SEARCH_FOCUS: return state.set('focused', true) case constants.SEARCH_BLUR: return state.set('focused', false) case constants.CHANGE_LIST: return state.set('list', fromJS(action.data)) //用fromJS包裹action.data后,控制台仍报错... } return state }
//src/common/header/store/actionCreators import * as constants from './constants' import axios from 'axios' import {fromJS} from 'immutable' const changeList = (data)=>({ type: constants.CHANGE_LIST, data: fromJS(data) //在data属性上调用了fromJS; }) export const searchFocus = ()=>({ type: constants.SEARCH_FOCUS }) export const searchBlur = ()=>({ type: constants.SEARCH_BLUR }) export const getList = ()=>{ return (dispatch)=>{ axios.get('./api/headerList.json').then((res)=>{ const data = res.data dispatch(changeList(data.data)) }).catch(()=>{ console.log('error') }) } }
登录后可查看更多问答,登录/注册
主流新技术 React-redux,React-router4,贯穿基础语法
5.5k 22
5.6k 20
2.0k 19
4.3k 17
1.4k 13