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

鍍金池/ 問答/HTML/ Cannot read property 'length' of undefin

Cannot read property 'length' of undefined

containers

import React,{Component} from 'react';
import {connect} from 'react-redux';
import actions from '../actions';
import {bindActionCreators} from 'redux';
class Counter extends Component {
    render() {

        let {counter, actions} = this.props;

        return (
            <div>
                <p>次數(shù):{counter}</p>
                <button onClick={actions.increment}>加</button>
                <button onClick={actions.decrement}>減</button>
                <button onClick={actions.undo}>撤銷</button>
                <button onClick={actions.redo}>重做</button>
            </div>
        );
    }
}
const mapStateToProps = state => ({
    counter: state.counter.present
});

const mapDispatchToProps = dispatch => ({
    actions: bindActionCreators(actions, dispatch)
});
export default connect(mapStateToProps, mapDispatchToProps)(Counter);

action

export const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER';
export const UNDO_COUNTER = 'UNDO_COUNTER';
export const REDO_COUNTER = 'REDO_COUNTER';

let actions = {
    increment: () => ({
        type: INCREMENT_COUNTER
    }),
    decrement: () => ({
        type:DECREMENT_COUNTER
    }),
    undo:()=>({
        type:UNDO_COUNTER
    }),
    redo:()=>({
        type:REDO_COUNTER
    })
};
export default actions;

reducers

import {combineReducers} from 'redux';
import undoable,{includeAction} from 'redux-undo';
import counter from './counter';
import {INCREMENT_COUNTER,DECREMENT_COUNTER,UNDO_COUNTER,REDO_COUNTER} from '../actions';

const rootReducer=combineReducers({
    counter:undoable(counter,{
        filter:includeAction([INCREMENT_COUNTER,DECREMENT_COUNTER]),
        limit:10,
        debug:true,
        undoType:UNDO_COUNTER,
        redoType:REDO_COUNTER
    })
});
export default rootReducer;
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../actions';
export default function counter(state=0,action) {
    switch (action.type) {
        case INCREMENT_COUNTER:
            return state + 1;
        case DECREMENT_COUNTER:
            return state - 1;
        default:
            return state;
    }
}

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import Counter from './containers/Counter';
import rootReducer from './reducers';

const store = createStore(rootReducer,applyMiddleware(thunk));
const rootEl = document.getElementById('root');

ReactDOM.render(
    <Provider store={store}>
        <Counter />
    </Provider>, rootEl);

為什么老是報(bào)錯(cuò) Cannot read property 'length' of undefined
圖片描述

回答
編輯回答
陌璃

const store = createStore(rootReducer,

compose(applyMiddleware(thunk),window.devToolsExtension?window.devToolsExtension():f=>f)

);

2017年4月10日 08:58
編輯回答
兔囡囡

js或者頁面使用了某個(gè)變量的length,那個(gè)變量沒拿到值,是undefined,undefined.length就會(huì)報(bào)錯(cuò);看是不是index.js中的。

2017年9月2日 04:13