在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ reducer有返回值,而且無論reducer的state怎么變化,但是打印的s

reducer有返回值,而且無論reducer的state怎么變化,但是打印的store時鐘為空的{}

其實有好幾個問,卡了好久了不知道什么原因
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...

回答
編輯回答
互擼娃
console.log("================監(jiān)聽store變化:"+JSON.stringify(store));

變化是變化了,不過姿勢不對。

store.subscribe(() => {
  console.log(store.getState()); // 返回應(yīng)用當(dāng)前的狀態(tài)樹
}
2018年7月5日 06:45
編輯回答
荒城

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,
});
2017年7月17日 05:39