Action does not trigger a reducer in React + Redux

后端 未结 2 1846
心在旅途
心在旅途 2021-01-18 02:32

I\'m working on a react-redux app and for some reason the action I call does not reach the reducer (in which I currently only have a log statement). I have attached the code

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-18 02:44

    Recently I faced issue like this and found that I had used action import but it has to come from props. Check out all uses of toggleAddContactModal. In my case I had missed toggleAddContactModal from destructuring statement which caused this issue.

    import React from 'react'
    import ReactDOM from 'react-dom'
    import Modal from 'react-modal'
    import { bindActionCreators } from 'redux'
    import { connect } from 'react-redux'
    import {
      fetchContacts,
      addContact,
      toggleAddContactModal
    } from '../../modules/contacts'
    import ContactList from "../../components/contactList";
    
    Modal.setAppElement('#root')
    
    class Contacts extends React.Component {
      componentDidMount(){
        this.props.fetchContacts();
      }
      render(){
        const {fetchContacts, isFetching, contacts, 
          error, isAdding, addContact, isRegisterModalOpen,
          toggleAddContactModal} = this.props;
        let firstName;
        let lastName;
        const handleAddContact = (e) => {
          e.preventDefault();
          if (!firstName.value.trim() || !lastName.value.trim()) {
            return
          }
          addContact({ firstName : firstName.value, lastName: lastName.value});
        };
    
        return (
          

    Contacts

    (firstName = node)} > (lastName = node)} >

    {error}

    Total {contacts.length} contacts

    ); } } const mapStateToProps = ({ contactInfo }) => { console.log(contactInfo) return ({ isAdding: contactInfo.isAdding, error: contactInfo.error, contacts: contactInfo.contacts, isFetching: contactInfo.isFetching, isRegisterModalOpen: contactInfo.isRegisterModalOpen }); } const mapDispatchToProps = dispatch => bindActionCreators( { fetchContacts, addContact, toggleAddContactModal }, dispatch ) export default connect( mapStateToProps, mapDispatchToProps )(Contacts)

提交回复
热议问题