为什么在我的redux应用程序中状态始终为null?

时间:2018-12-19 01:55:21

标签: javascript reactjs redux react-redux state

我正在构建一个非常简单的联系人应用程序,以熟悉React和Redux。现在,我正在尝试构建添加联系人的功能。我以为我已正确设置了动作和减速器,但当前传递给减速器的状态为null。我希望当前的联系人列表处于通过状态。如果在执行reducer后记录状态,甚至没有得到我希望添加的一个联系人,则该状态仍然为空。任何帮助表示赞赏!

我的代码如下

操作:

function addContact(contact) {
  console.log("it gets to the action");
  return {
    type: 'ADD_CONTACT',
    payload: contact
  }
}

export default addContact;

AddContacts Reducer:

export default function (state = {}, action) {
  console.log(state);

  switch (action.type) {
    case 'ADD_CONTACT':
      return Object.assign({}, state, {
        contacts: [
          ...state.contacts,
          {
            name: action.payload.name,
            phone: action.payload.phone
          }
        ]
      })
  }
  return state;
}

调用此操作的组件:

import React, { Component } from 'react'
import AddContactButton from './AddContactButton';
import { connect } from 'react-redux'
import addContact from '../actions/action_add_contact'
import { bindActionCreators } from 'redux'

class AddContactModal extends Component {

  constructor() {
    super();
    this.state = {firstName: "", phone: ""};
  }

  handleNameChange(event) {
    this.setState({firstName: event.target.value})
  }

  handlePhoneChange(event) {
    this.setState({phone: event.target.value});
  }

  onClick() {
    console.log($r.store.getState());
    this.props.addContact({"name": this.state.firstName, "phone": this.state.phone});
  }

  render() {

    return(
      <div>
        <input type="text" className="name" placeholder="Contact Name" onChange={(event) => this.handleNameChange(event)}/>
        <input type="text" className="phone" placeholder="Contact Phone" onChange={(event) => this.handlePhoneChange(event)}/>
        <AddContactButton firstName={this.state.firstName} firstName={this.state.phone} onClick={() => this.onClick()}/>
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {
    contacts: state.contacts
  };
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({ addContact: addContact }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(AddContactModal)

创建商店:

const createStoreWithMiddleware = applyMiddleware()(createStore);
const initialState = {
  "contacts": [
    {
      "name": "Miguel Camilo",
      "phone": "123456789"
    },
    {
      "name": "Peter",
      "phone": "883292300348"
    },
    {
      "name": "Jessica",
      "phone": "8743847638473"
    },
    {
      "name": "Michael",
      "phone": "0988765553"
    }
  ],
  "activeContact": null
};

ReactDOM.render(
  <Provider store={ createStoreWithMiddleware(reducers, initialState) }>
   <App />
  </Provider>
, document.querySelector('.container'));

Contacts reducer,它仅返回硬编码的联系人列表以设置初始状态:

export default function () {
  return [{
    "name": "Miguel Camilo",
    "phone": "123456789"
  },{
    "name": "Peter",
    "phone": "883292300348"
  },{
    "name": "Jessica",
    "phone": "8743847638473"
  },{
    "name": "Michael",
    "phone": "0988765553"
  }];
}

我很确定我的问题是由于我在组件中使用mapStateToProps和mapDispatchToProps而引起的,当我查看redux状态时,我看到它添加了一个我不想要的集合“ addContacts”?它不应该只是从操作中获取有效负载并将其添加到“联系人”集合中吗?

1 个答案:

答案 0 :(得分:1)

我认为您错过了发运:

    function addContact(contact) {
        console.log("it gets to the action");
        dispatch({
           type: 'ADD_CONTACT',
           payload: contact
        });
   }

   export default addContact;