其实有好几个问,卡了好久了不知道什么原因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.jsReactDOM.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,});
添加回答
举报
0/150
提交
取消