为了账号安全,请及时绑定邮箱和手机立即绑定

reducer有返回值,而且无论reducer的state怎么变化,但是打印的store时钟为空的{}

reducer有返回值,而且无论reducer的state怎么变化,但是打印的store时钟为空的{}

慕容3067478 2019-05-21 10:37:02
其实有好几个问,卡了好久了不知道什么原因1.两个reducer的state都有值的情况下store.subscribe(()=>{console.log("================监听store变化:"+JSON.stringify(store));});打印的值一直是空的{}。2.点击导航时候在Mount组件的时候去请求数据,然后执行store.dispatch(initUserAction(res)),初始化reducer的state,state有数据,但是store.subscribe里面答应的store一直是空的。这个是加载组件的时候初始化的代码componentWillMount(){const{store}=this.context;fetch("http://localhost:3001/book").then(res=>res.json()).then(res=>{store.dispatch(initBookAction(res));});}这个是reducer的代码constinitialState={data:[]};functionbookReducer(state=initialState,action){switch(action.type){case'INIT_BOOK_ACTION':console.log("=====初始化bookReducer:"+JSON.stringify(state));returnObject.assign({},state,{data:[...action.payload]})case'ADD_BOOK_ACTION':returnObject.assign({},state,{data:state.data.push(action.payload)})case'DELETE_BOOK_ACTION':returnObject.assign({},state,{data:state.data.filter(item=>item.id!=action.payload.id)})case'UPDATE_BOOK_ACTION':returnObject.assign({},state,{data:state.data.map(item=>{if(item.id==action.payload.id){returnObject.assign({},item,action.payload);}else{returnitem;}})})default:returnstate}}exportdefaultbookReducer;3.我加载的table的数据是mapStateToProps传递过来的数据,既然store为空,为什么container组件中给mapStateToProps传递的state依然可以取到数据state.bookReducer.date,然后在table中展示出来数据呢?这个是rendertable的主要代码render(){const{bookList,deleteBook}=this.props;//connect传递的propsconst{title,visible,confirmLoading}=this.state;//console.log("===================booklistprops:"+JSON.stringify(this.props));constcolumns=[{title:'图书编号',dataIndex:'id'},{title:'名称',dataIndex:'name'},{title:'价格',dataIndex:'price'},{title:'借阅人编号',dataIndex:'owner_id'},{title:'操作',render:(text,record)=>(this.editHandle(record)}>编辑deleteBook(record)}>删除)}];return(this.cancelHandle()}footer={null}>);}这个是创建container组件的代码import{connect}from'react-redux';importBookListfrom'../components/BookList';import{deleteBookAction,addBookAction,updateBookAction}from'../actions/bookActions';constmapStateToProps=(state)=>{return{bookList:state.bookReducer.data};}constmapDispatchToProps=(dispatch)=>{return{deleteBook:(id)=>{dispatch(deleteBookAction(id))},addBook:(data)=>{dispatch(addBookAction(data))},editBook:(data)=>{dispatch(updateBookAction(data))}}}constBookListContainer=connect(mapStateToProps,mapDispatchToProps)(BookList);exportdefaultBookListContainer;代码有点多我把github地址粘上来,麻烦各位大神帮我看一下,谢谢~github地址:https://github.com/hesisi/rea...
查看完整描述

2 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

reducer没有注册到store
你要从“本源”上去看问题,先看看你的entry.js
ReactDOM.render(
//routes
去看看store从哪里来的。最后追溯到一个整个应用的reducer文件的汇总注册文件reducer.js:
import{combineReducers}from"redux";
import{routerReducer}from"react-router-redux";
import*asyourComponentReducerfrom'./path/reducers.ts';
exportdefaultcombineReducers({
routing:routerReducer,
...yourComponentReducer,
});
                            
查看完整回答
反对 回复 2019-05-21
  • 2 回答
  • 0 关注
  • 548 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信