React Native with Redux:状态更改未显示在控制台中

React Native with Redux : state changes not showing in console

当我将 console.log('test') 语句放入我的 reducer 时,我可以在调用操作时在控制台中看到它们。但是我在控制台中没有看到 redux "NEXT STATE"/"PREV STATE" 的东西。

有什么基本的东西我可能会遗漏吗?

在下面的代码中 - 我并没有尝试实现任何实际功能,我只是尝试设置 redux 并在控制台中查看状态变化(所以我知道我在正确的道路上)。


容器

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);

减速器

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;
  }
}

操作数

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

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

当我意识到需要单独安装https://github.com/fcomb/redux-logger时解决了。

正如您已经确定的那样,您需要安装 redux-logger 中间件。 https://github.com/evgenyrodionov/redux-logger

来自他们的文档:

import { applyMiddleware, createStore } from 'redux';

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

我只在开发环境中这样做

const middlewares = [];

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