D.R D.R - 4 months ago 44
React JSX Question

react js mapStateToProps triggers Uncaught TypeError: Cannot read property 'map' of undefined

I have an index.js which renders all tables in the database.

_renderAllTables() {
const { fetching } = this.props;

let content = false;

if(!fetching) {
content = (
<div className="tables-wrapper">

return (
<header className="view-header">
<h3>All Tables</h3>

_renderTables(tables) {

return tables.map((table) => {
return <Table
{...table} />;

render() {
return (
<div className="view-container tables index">
const mapStateToProps = (state) => (

export default connect(mapStateToProps)(HomeIndexView);

I changed mapStateToProps from above code to below code.

const mapStateToProps = (state) => ({
tables: state.tables,
currentOrder: state.currentOrder,

The reason why I changed code is that I want to use one of state of currentOrder. I have a state which shows whether table is busy or not. So in order to use that I added currentOrder in mapStateToProps. However, it triggers Uncaught TypeError: Cannot read property 'map' of undefined..

How can I use states from other components? any suggestions for that??

Thanks in advance..


const initialState = {
allTables: [],
showForm: false,
fetching: true,
formErrors: null,

export default function reducer(state = initialState, action = {}) {
case Constants.TABLES_FETCHING:
return {...state, fetching: true};

case Constants.TABLES_RECEIVED:
return {...state, allTables: action.allTables, fetching: false};

case Constants.TABLES_SHOW_FORM:
return {...state, showForm: action.show};

case Constants.TALBES_RESET:
return initialState;

return { ...state, formErrors: action.errors };

return state;


Your problem is that before fetching successfully tables, your component is rendered with state.tables is undefined.

First of all, best practice is to use selectors rather than json path like state.tables, to be in a separate selectors.js file using reselect lib as follow:

import { createSelector } from 'reselect';

const tablesSelector = state => state.tables;

export default {

Second, you need to add reducer for, let's assume, FETCH_ALL_TABLES action using combineReducers from redux lib and most important to initialize tables array with [] before the action is dispatched so as to be defined as follow:

import {combineReducers} from 'redux';

function tables(state = [], {type, payload}) {
  switch (type) {
    case 'FETCH_ALL_TABLES':
      return [

  return state;

export default combineReducers({

and in your index.js, may be want update it to:

import selector from './selector';


function mapStateToProps(state) {
  return {
    tables: selector.tablesSelector(state),