Troubleshooting

This is a place to share common problems and solutions to them.The examples use React, but you should still find them useful if you use something else.

Nothing happens when I dispatch an action

Sometimes, you are trying to dispatch an action, but your view does not update. Why does this happen? There may be several reasons for this.

Never mutate reducer arguments

It is tempting to modify the state or action passed to you by Redux. Don't do this!

Redux assumes that you never mutate the objects it gives to you in the reducer. Every single time, you must return the new state object. Even if you don't use a library like Immutable, you need to completely avoid mutation.

Immutability is what lets react-redux efficiently subscribe to fine-grained updates of your state. It also enables great developer experience features such as time travel with redux-devtools.

For example, a reducer like this is wrong because it mutates the state:

  1. function todos(state = [], action) {
  2. switch (action.type) {
  3. case 'ADD_TODO':
  4. // Wrong! This mutates state
  5. state.push({
  6. text: action.text,
  7. completed: false
  8. })
  9. return state
  10. case 'COMPLETE_TODO':
  11. // Wrong! This mutates state[action.index].
  12. state[action.index].completed = true
  13. return state
  14. default:
  15. return state
  16. }
  17. }

It needs to be rewritten like this:

  1. function todos(state = [], action) {
  2. switch (action.type) {
  3. case 'ADD_TODO':
  4. // Return a new array
  5. return [
  6. ...state,
  7. {
  8. text: action.text,
  9. completed: false
  10. }
  11. ]
  12. case 'COMPLETE_TODO':
  13. // Return a new array
  14. return state.map((todo, index) => {
  15. if (index === action.index) {
  16. // Copy the object before mutating
  17. return Object.assign({}, todo, {
  18. completed: true
  19. })
  20. }
  21. return todo
  22. })
  23. default:
  24. return state
  25. }
  26. }

It's more code, but it's exactly what makes Redux predictable and efficient. If you want to have less code, you can use a helper like React.addons.update to write immutable transformations with a terse syntax:

  1. // Before:
  2. return state.map((todo, index) => {
  3. if (index === action.index) {
  4. return Object.assign({}, todo, {
  5. completed: true
  6. })
  7. }
  8. return todo
  9. })
  10. // After
  11. return update(state, {
  12. [action.index]: {
  13. completed: {
  14. $set: true
  15. }
  16. }
  17. })

Finally, to update objects, you'll need something like _.extend from Underscore, or better, an Object.assign polyfill.

Make sure that you use Object.assign correctly. For example, instead of returning something like Object.assign(state, newData) from your reducers, return Object.assign({}, state, newData). This way you don't override the previous state.

You can also enable the object spread operator proposal for a more succinct syntax:

  1. // Before:
  2. return state.map((todo, index) => {
  3. if (index === action.index) {
  4. return Object.assign({}, todo, {
  5. completed: true
  6. })
  7. }
  8. return todo
  9. })
  10. // After:
  11. return state.map((todo, index) => {
  12. if (index === action.index) {
  13. return { ...todo, completed: true }
  14. }
  15. return todo
  16. })

Note that experimental language features are subject to change.

Also keep an eye out for nested state objects that need to be deeply copied. Both _.extend and Object.assign make a shallow copy of the state. See Updating Nested Objects for suggestions on how to deal with nested state objects.

Don't forget to call dispatch(action)

If you define an action creator, calling it will not automatically dispatch the action. For example, this code will do nothing:

TodoActions.js

  1. export function addTodo(text) {
  2. return { type: 'ADD_TODO', text }
  3. }

AddTodo.js

  1. import React, { Component } from 'react'
  2. import { addTodo } from './TodoActions'
  3. class AddTodo extends Component {
  4. handleClick() {
  5. // Won't work!
  6. addTodo('Fix the issue')
  7. }
  8. render() {
  9. return <button onClick={() => this.handleClick()}>Add</button>
  10. }
  11. }

It doesn't work because your action creator is just a function that returns an action. It is up to you to actually dispatch it. We can't bind your action creators to a particular Store instance during the definition because apps that render on the server need a separate Redux store for every request.

The fix is to call dispatch() method on the store instance:

  1. handleClick() {
  2. // Works! (but you need to grab store somehow)
  3. store.dispatch(addTodo('Fix the issue'))
  4. }

If you're somewhere deep in the component hierarchy, it is cumbersome to pass the store down manually. This is why react-redux lets you use a connect higher-order component that will, apart from subscribing you to a Redux store, inject dispatch into your component's props.

The fixed code looks like this:

AddTodo.js

  1. import React, { Component } from 'react'
  2. import { connect } from 'react-redux'
  3. import { addTodo } from './TodoActions'
  4. class AddTodo extends Component {
  5. handleClick() {
  6. // Works!
  7. this.props.dispatch(addTodo('Fix the issue'))
  8. }
  9. render() {
  10. return <button onClick={() => this.handleClick()}>Add</button>
  11. }
  12. }
  13. // In addition to the state, `connect` puts `dispatch` in our props.
  14. export default connect()(AddTodo)

You can then pass dispatch down to other components manually, if you want to.

Make sure mapStateToProps is correct

It's possible you're correctly dispatching an action and applying your reducer but the corresponding state is not being correctly translated into props.

Something else doesn't work

Ask around on the #reduxReactiflux Discord channel, or create an issue.If you figure it out, edit this document as a courtesy to the next person having the same problem.