React Redux 无法让 reducer 接收动作

React Redux cant get reducer to pick up actions

我一直在学习 redux 和 React,并且正在构建待办事项列表。我一直在阅读和查看不同的文章,但无法弄清楚我的设置中缺少什么。

目前您可以通过输入法添加待办事项。在按下输入时,它会发送一个带有待办事项文本的 addTodo 操作。

我期待减速器看到动作类型并更新状态,但它从来没有。我错过了什么?

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import reducer from './reducer.js';
import TodoList from './containers/container.js';

const store = createStore(reducer);

ReactDOM.render(
  <Provider store={store}>
    <TodoList />
  </Provider>,
document.getElementById('app'));

actions.js

var uuid = require('node-uuid');

export function addTodo(text) {
  console.log('action addTodo', text);
  return {
    type: 'ADD_TODO',
    payload: {
      id: uuid.v4(),
      text: text
    }
  };
}

TodoListComponent.jsx

import React from 'react';
import TodoComponent from './TodoComponent.jsx';
import { addTodo } from '../actions/actions.js'

export default class TodoList extends React.Component {

  render () {
    const { todos } = this.props;

    return (
      <div>
        <input type='text' placeholder='Add todo' onKeyDown={this.onSubmit} />
        <ul>
          {todos.map(c => (
            <li key={t.id}>
              <TodoComponent todo={t} styleName='large' />
            </li>
          ))}
        </ul>
      </div>
    )
  }

  onSubmit(e) {
    const input = e.target;
    const text = input.value;
    const isEnterKey = (e.which === 13);

    if (isEnterKey) {
      input.value = '';
      addTodo(text);
    }
  }

}

TodoComponent.jsx

import React from 'react';
import CSSModules from 'react-css-modules';
import styles from './style.css';

export default class TodoComponent extends React.Component {

  render () {
    const { todo } = this.props;

    return (
      <div styleName='large'>{todo.text}</div>
    )
  }
}

export default CSSModules(TodoComponent, styles);

container.js

import { connect } from 'react-redux';
import TodoList from '../components/TodoListComponent.jsx';
import { addTodo } from '../actions/actions.js';

const mapStateToProps = (state) => {
   return {
      todos: state
   }
};

const mapDispatchToProps = (dispatch) => {
   return {
      addTodo: text => dispatch(addTodo(text))
   }
};

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

reducer.js

import { List, Map } from 'immutable';

const init = List([]);

export default function(todos = init, action) {

  console.log('reducer action type', action.type);

  switch(action.type) {
    case 'ADD_TODO':
      console.log('ADD_TODO');
      return todos.push(Map(action.payload));
    default:
      return todos;
  }
}

在您的 TodoListComponent 中,您直接从操作文件中导入操作,但实际上您想要使用映射的操作来分派并在容器中作为 属性 传递。这解释了为什么您看到来自操作的日志,而不是来自 reducer 的日志,因为操作永远不会被分派到商店。

所以你的 TodoListComponent 应该是:

import React from 'react';
import TodoComponent from './TodoComponent.jsx';

export default class TodoList extends React.Component {

  render () {
    const { todos } = this.props;

    return (
      <div>
        <input type='text' placeholder='Add todo' onKeyDown={this.onSubmit} />
        <ul>
          {todos.map(c => (
            <li key={t.id}>
              <TodoComponent todo={t} styleName='large' />
            </li>
          ))}
        </ul>
      </div>
    )
  }

  onSubmit(e) {
    const input = e.target;
    const text = input.value;
    const isEnterKey = (e.which === 13);

    if (isEnterKey) {
      input.value = '';
      this.props.addTodo(text);
    }
  }

}
import React from 'react';
import TodoComponent from './TodoComponent.jsx';

export default class TodoList extends React.Component {

  render () {
    const { todos } = this.props;

    return (
      <div>
        <input type='text' placeholder='Add todo' onKeyDown={this.onSubmit.bind(this)} />
        <ul>
          {todos.map(c => (
            <li key={t.id}>
              <TodoComponent todo={t} styleName='large' />
            </li>
          ))}
        </ul>
      </div>
    )
  }

  onSubmit(e) {
    // use the addTodo passed via connect
    const { addTodo } = this.props;
    const input = e.target;
    const text = input.value;
    const isEnterKey = (e.which === 13);

    if (isEnterKey) {
      input.value = '';
      addTodo(text);
    }
  }

}