nick722
nick722

Reputation: 1019

Why my state is underfined even when I've set it as an empty array by default?

I've got "Cannot read property 'map' of undefined" error on the line

{props.decks.map((deck, i) => 

of my Sidebar component

import React from 'react';
import ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import { addDeck, showAddDeck, hideAddDeck } from '../actions';

const mapStateToProps = ({decks, addingDeck}) => ({
  decks,
  addingDeck
});

const mapDispatchToProps = dispatch => ({
  addDeck: name => dispatch(addDeck(name)),
  showAddDeck: () => dispatch(showAddDeck()),
  hideAddDeck: () => dispatch(hideAddDeck())
});

class Sidebar extends React.Component {
  constructor(props) {
    super(props);
    this.createDeck = this.createDeck.bind(this);
  }
  componentDidUpdate() {
    var el = ReactDOM.findDOMNode(this.refs.add);
    if (el) el.focus();
  }
  render() {
    let props = this.props;
    return  (
      <div className='sidebar'>
        <h2> All Decks </h2>
        <button onClick={ e => this.props.showAddDeck() }>
          New Decks
        </button>
        <ul>
          {props.decks.map((deck, i) => 
            <li key={i}> {deck.name} </li>
            )}
        </ul>
        {props.addingDeck && <input ref='add' onKeyPress={this.createDeck} />}
      </div>);
  }
  createDeck(evt) {
    const ENTER = 13;
    if (evt.which !== ENTER) return;
    var name = ReactDOM.findDOMNode(this.refs.add).value;
    this.props.addDeck(name);
    this.props.hideAddDeck();
  }
};

export default connect(mapStateToProps, mapDispatchToProps)(Sidebar);

I've set the state as an empty array by default in reducers.js

so I can't understand why my props.decks is underfined

is it something with ES6?

export const decks = (state, action) => {
  switch (action.type) {
    case 'ADD_DECK':
      let newDeck = { name: action.data, id: +new Date };
      return state.concat([newDeck]);
    default: 
      return state || [];
  }
};

Here is my index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { Router, Route, browserHistory } from 'react-router';
import { syncHistoryWithStore, routerReducer, ConnectedRouter  } from 'react-router-redux';
import * as reducers from './reducers';
import './index.css';
import App from './components/App';


const store = createStore(combineReducers({reducers,
    routing: routerReducer}));
const history = syncHistoryWithStore(browserHistory, store);

function run () {
  let state = store.getState();
  ReactDOM.render(
    <Provider store={store}>
      <Router history={history}>
        <Route path='/' component={App}></Route>
      </Router>
    </Provider>, document.getElementById('root'));
}

run();
store.subscribe(run);

and App.js

import React from 'react';
import Sidebar from './Sidebar';

const App = ( props ) => {
  return (
    <div className='app'>
      <Sidebar />
      { props.children }
    </div>);
};

export default App;

and package.json

{
  "name": "flashcard-app-3",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babelify": "^8.0.0",
    "fuzzysearch": "^1.0.3",
    "history": "^4.7.2",
    "live-server": "^1.2.0",
    "prop-types": "^15.6.0",
    "react": "^15.0.0",
    "react-dom": "^15.0.0",
    "react-redux": "^4.4.1",
    "react-router": "^2.0.1",
    "react-router-dom": "^4.2.2",
    "react-router-redux": "^4.0.1",
    "redux": "^3.3.1",
    "watchify": "^3.9.0",
    "react-scripts": "1.0.17"
  },
  "scripts": {
    "start": "set PORT=3001 && react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

Upvotes: 0

Views: 70

Answers (1)

floriangosse
floriangosse

Reputation: 1123

The problem in the line where you create the store. If you want to access the decks in the state via state.decks you should use

combineReducers({ routing: routerReducer, ...reducers })

This way the properties of reducers will be added to the object you pass to combineReducers. Otherwise you have to access your state like this: state.reducers.decks.

If you want to read more about it check out MDN.

Upvotes: 1

Related Questions