其實(shí)有好幾個(gè)問(wèn),卡了好久了不知道什么原因1.兩個(gè)reducer的state都有值的情況下store.subscribe(()=>{console.log("================監(jiān)聽(tīng)store變化:"+JSON.stringify(store));});打印的值一直是空的{}。2.點(diǎn)擊導(dǎo)航時(shí)候在Mount組件的時(shí)候去請(qǐng)求數(shù)據(jù),然后執(zhí)行store.dispatch(initUserAction(res)),初始化reducer的state,state有數(shù)據(jù),但是store.subscribe里面答應(yīng)的store一直是空的。這個(gè)是加載組件的時(shí)候初始化的代碼componentWillMount(){const{store}=this.context;fetch("http://localhost:3001/book").then(res=>res.json()).then(res=>{store.dispatch(initBookAction(res));});}這個(gè)是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的數(shù)據(jù)是mapStateToProps傳遞過(guò)來(lái)的數(shù)據(jù),既然store為空,為什么container組件中給mapStateToProps傳遞的state依然可以取到數(shù)據(jù)state.bookReducer.date,然后在table中展示出來(lái)數(shù)據(jù)呢?這個(gè)是rendertable的主要代碼render(){const{bookList,deleteBook}=this.props;//connect傳遞的propsconst{title,visible,confirmLoading}=this.state;//console.log("===================booklistprops:"+JSON.stringify(this.props));constcolumns=[{title:'圖書(shū)編號(hào)',dataIndex:'id'},{title:'名稱(chēng)',dataIndex:'name'},{title:'價(jià)格',dataIndex:'price'},{title:'借閱人編號(hào)',dataIndex:'owner_id'},{title:'操作',render:(text,record)=>(this.editHandle(record)}>編輯deleteBook(record)}>刪除)}];return(this.cancelHandle()}footer={null}>);}這個(gè)是創(chuàng)建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;代碼有點(diǎn)多我把github地址粘上來(lái),麻煩各位大神幫我看一下,謝謝~github地址:https://github.com/hesisi/rea...
reducer有返回值,而且無(wú)論reducer的state怎么變化,但是打印的store時(shí)鐘為空的{}
慕容3067478
2019-05-21 10:37:02