Elliot
Elliot

Reputation: 13845

React Native with Redux : state changes not showing in console

When I put console.log('test') statements in my reducer, I can see them in the console when the actions are called. But I'm not seeing the redux "NEXT STATE"/ "PREV STATE" stuff in the console.

Is there anything basic I could be missing?

In the code below - I'm not trying to make any real functionality happen, I'm just trying to setup redux and see the state change in the console (so I know I'm on the correct path).


Container

import React, { PropTypes } from 'react-native';
import Header from './Header';
import { connect } from 'react-redux';
import { leave } from './actions';
import { join } from './actions';

const mapStateToProps = (state) => {
  return {
    in: state.in
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    join: (id) => {
      dispatch(join(id))
    },
    leave: (id) => {
      dispatch(leave(id))
    }
  }
}

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

Reducer

export default function Header(state = { in: false }, action = {}) {
  switch (action.type) {
    case 'LEAVE':
      return {
        ...state,
        in: false
      }
    case 'JOIN':
      console.log(state);
      console.log(action);
      console.log('join');
      return {
        ...state,
        in: true
      }
    default:
      return state;
  }
}

Actions

export const join = (id) => {
  return {
    type: 'JOIN',
    payload: {
      in: true
    }
  }
}

export const leave = (id) => {
  return {
    type: 'LEAVE',
    payload: {
      in: false
    }
  }
}

Upvotes: 3

Views: 2895

Answers (2)

Elliot
Elliot

Reputation: 13845

It solved when I realized that https://github.com/fcomb/redux-logger is needed to be installed separately.

Upvotes: 4

Powderham
Powderham

Reputation: 1640

As you've already identified, you need to install redux-logger middleware. https://github.com/evgenyrodionov/redux-logger

From their docs:

import { applyMiddleware, createStore } from 'redux';

// Logger with default options
import logger from 'redux-logger'
const store = createStore(
  reducer,
  applyMiddleware(logger)
)

I only do this when in a dev environment

const middlewares = [];

if (process.env.NODE_ENV === 'dev') {
  middlewares.push(logger);
}
const store = createStore(
  reducer,
  applyMiddleware(...middlewares)
)

Upvotes: 0

Related Questions