question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redux's combineReducers() not working with Next

See original GitHub issue

Examples bug report

Example name

with-redux

Describe the bug

I am trying to expand upon this example with the use of combineReducers(). The example places reducers, actions, etc. in the store.js file for simplicity/readability. However, in a production example you may want to separate actions and reducers.

I have had no problem separating actions, but in order to separate reducers I need to make use of combineReducers(). Exporting a single reducer works perfectly, but when using the combine method it seems to forget about its initial state and not provide access to a defined state.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

reducers/teamReducer.js

const initialState = {
  team: [],
};

export default (state = initialState, action) => {
  switch (action.type) {
    case SET_TEAM:
      return Object.assign({}, state, {
        team: action.team,
      });
    default:
      return state;
  }
};

reducers/index.js

import { combineReducers } from 'redux';

import Team from './Team';

export default combineReducers({
  Team
}); // when trying to access state variables they are undefined? why?

// export default Team; **THIS WORKS - why not with combineReducers?**

store.js

import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import reducer from './reducers';

export default function initializeStore(initialState) {
  return createStore(
    reducer,
    initialState,
    composeWithDevTools(applyMiddleware()),
  );
}

team.js

import React from 'react';
import { connect } from 'react-redux';
import { setTeam } from '../actions';

class Team extends React.Component {
    async componentDidMount() {
        const teamArray = await getTeamFromAsyncFunc();
        this.props.setTeam(teamArray);
    }

    render() {
        <div>
               {this.props.team.map .... UNDEFINED WHEN USING combineReducers()}
        </div>
    }
}

const mapStateToProps = state => ({
  routes: state.team,
});

const mapDispatchToProps = dispatch => ({
  setTeam: team => dispatch(setTeam(team)),
});

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

Expected behavior

I expect to be able to access the team array from props in the same way I can when exporting a single reducer without combineReducers().

When I try to access this.props.team after exporting with combineReducers() it returns undefined.

System information

  • OS: macOS Mojave
  • Browser (if applies) Chrome
  • Version of Next.js: 8.0.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
us-22commented, Aug 6, 2019

@jskidd3 I have the same issue. how you manage to solve this.

2reactions
jskidd3commented, Jun 4, 2019

@alanhr Have updated with the actual page now. Hopefully this will help. Thanks 😊

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux's combineReducers() not working with Next #7498
I have had no problem separating actions, but in order to separate reducers I need to make use of combineReducers() . Exporting a...
Read more >
combineReducers() not working in React Redux
When you use combineRreducers , your store object will have the same keys as the object you pass in with the reducer return...
Read more >
Using combineReducers
First and foremost, combineReducers is simply a utility function to simplify the most common use case when writing Redux reducers. You are not...
Read more >
Next.js — Setting up a Redux store with combineReducers()
Here is a short guide about setting up a Redux store with combineReducers() in your Next.js project. This is a guide for beginners....
Read more >
4.2.1.2 - Combining Redux Reducers
Then we will learn how to use Redux's combineReducers() function to combine all of our individual reducers into a single root reducer. Then,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found