其實有好幾個問,卡了好久了不知道什么原因
1.兩個reducer的state都有值的情況下
store.subscribe(() => {
console.log("================監(jiān)聽store變化:"+JSON.stringify(store));
});
打印的值一直是空的{}。
2.點擊導(dǎo)航時候在Mount組件的時候去請求數(shù)據(jù),然后執(zhí)行store.dispatch(initUserAction(res)),初始化reducer的state,state有數(shù)據(jù),但是store.subscribe里面答應(yīng)的store一直是空的。
這個是加載組件的時候初始化的代碼
componentWillMount(){
const {store} = this.context;
fetch("http://localhost:3001/book")
.then(res => res.json())
.then(res => {
store.dispatch(initBookAction(res));
});
}
這個是reducer的代碼
const initialState = {
data : []
};
function bookReducer(state=initialState ,action){
switch(action.type){
case 'INIT_BOOK_ACTION':
console.log("=====初始化bookReducer:"+JSON.stringify(state));
return Object.assign({},state,{
data : [...action.payload]
})
case 'ADD_BOOK_ACTION' :
return Object.assign({},state,{
data : state.data.push(action.payload)
})
case 'DELETE_BOOK_ACTION' :
return Object.assign({},state,{
data : state.data.filter(item => item.id != action.payload.id)
})
case 'UPDATE_BOOK_ACTION' :
return Object.assign({},state,{
data : state.data.map(item => {
if(item.id == action.payload.id){
return Object.assign({},item,action.payload);
}else{
return item;
}
})
})
default:
return state
}
}
export default bookReducer;
3.我加載的table的數(shù)據(jù)是mapStateToProps傳遞過來的數(shù)據(jù),既然store為空,為什么container組件中給mapStateToProps傳遞的state依然可以取到數(shù)據(jù)state.bookReducer.date,然后在table中展示出來數(shù)據(jù)呢?
這個是render table的主要代碼
render(){
const { bookList, deleteBook } = this.props; //connect傳遞的props
const { title,visible ,confirmLoading } = this.state;
//console.log("===================booklist props:"+JSON.stringify(this.props));
const columns = [{
title : '圖書編號',
dataIndex : 'id'
},{
title : '名稱',
dataIndex : 'name'
},{
title:'價格',
dataIndex:'price'
},{
title:'借閱人編號',
dataIndex:'owner_id'
},{
title:'操作',
render : (text,record) => (
<span type="ghost">
<Button size="small" onClick={() => this.editHandle(record)}>編輯</Button>
<Divider type="vertical" />
<Popconfirm title="確定要刪除嗎?" onConfirm={() => deleteBook(record)}>
<Button size="small" >刪除</Button>
</Popconfirm>
</span>
)
}];
return (
<div>
<div>
<SearchInput addHandle={this.addHandle.bind(this)}/>
</div>
<Table columns={columns} dataSource={bookList}/>
<Modal
title={title}
visible= {visible}
confirmLoading = {confirmLoading}
onCancel = {() => this.cancelHandle()}
footer = {null}
>
<FormLayout record={this.state.formData} comfirmHandle={this.comfirmHandle.bind(this)}/>
</Modal>
</div>
);
}
這個是創(chuàng)建container組件的代碼
import { connect } from 'react-redux';
import BookList from '../components/BookList';
import { deleteBookAction , addBookAction ,updateBookAction } from '../actions/bookActions';
const mapStateToProps = (state) => {
return {
bookList : state.bookReducer.data
};
}
const mapDispatchToProps = (dispatch) => {
return {
deleteBook : (id) => {
dispatch(deleteBookAction(id))
},
addBook : (data) => {
dispatch(addBookAction(data))
},
editBook : (data) => {
dispatch(updateBookAction(data))
}
}
}
const BookListContainer = connect(
mapStateToProps,
mapDispatchToProps
)(BookList);
export default BookListContainer;
代碼有點多我把github地址粘上來,麻煩各位大神幫我看一下,謝謝~
github地址:https://github.com/hesisi/rea...
reducer沒有注冊到store
你要從“本源”上去看問題,先看看你的entry.js
ReactDOM.render(
<Provider store={store}>
// routes
</Provider>
去看看store從哪里來的。
最后追溯到一個整個應(yīng)用的reducer文件的匯總注冊文件reducer.js:
import { combineReducers } from "redux";
import { routerReducer } from "react-router-redux";
import * as yourComponentReducer from './path/reducers.ts';
export default combineReducers({
routing: routerReducer,
...yourComponentReducer,
});
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進(jìn)“中國制造2025”,實現(xiàn)中華民族偉大復(fù)興的升級產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。