为什么在下面会引发TypeError:调度不是函数?

时间:2018-12-17 08:10:12

标签: javascript reactjs ecmascript-6

Contxet.js文件:-

import React, { Component } from "react";

const Context = React.createContext();
const reducer = (state, action) => {
  switch (action.type) {
    case "DELETE_CONTACT":
      return {
        ...state,
        contacts: state.contacts.filter(
          contact => contact.id !== action.payload
        )
      };
    default:
      return state;
  }
};

export class Provider extends Component {
  constructor() {
    super();
    this.state = {
      contacts: [
        {
          id: 1,
          name: "Person One",
          email: "person1@gmail.com",
          phone: "9999999999"
        },
        {
          id: 2,
          name: "Person Two",
          email: "person2@gmail.com",
          phone: "8888888888"
        },
        {
          id: 3,
          name: "Person 3",
          email: "person3@gmail.com",
          phone: "7777777777"
        }
      ],
      disptach: action => this.setState(state => reducer(action, state))
    };
  }

  render() {
    return (
      <Context.Provider value={this.state}>
        {this.props.children}
      </Context.Provider>
    );
  }
}

export const Consumer = Context.Consumer;

下面的Contact.js文件:-

import React, { Component } from "react";
import PropTypes from "prop-types";
import "./contact.css";
import { Consumer } from "../context";

class Contact extends Component {
  state = {
    showContactInfo: false
  };

  onDeleteClick = (id, dispatch) => {
    dispatch({ type: "DELETE_CONTACT", payload: id });
  };

  render() {
    const { id, name, email, phone } = this.props.contact;
    const { showContactInfo } = this.state;

    return (
      <Consumer>
        {value => {
          const { dispatch } = value;
          return (
            <div className="card card-body mb-3">
              <h4>
                {name}{" "}
                <i
                  onClick={() =>
                    this.setState({
                      showContactInfo: !this.state.showContactInfo
                    })
                  }
                  className="fas fa-sort-down"
                  style={{ cursor: "pointer" }}
                />
                <i
                  className="fas fa-times"
                  style={{ cursor: "pointer", float: "right", color: "red" }}
                  onClick={this.onDeleteClick.bind(this, id, dispatch)}
                />
              </h4>
              {showContactInfo ? (
                <ul className="list-group">
                  <li className="list-group-item">Email :- {email}</li>
                  <li className="list-group-item">Mobile :- {phone}</li>
                </ul>
              ) : null}
              <br />
            </div>
          );
        }}
      </Consumer>
    );
  }
}

Contact.propTypes = {
  contact: PropTypes.object.isRequired
};

export default Contact;

仅执行“删除操作”时,它提示分派不是功能。我刚刚开始学习React.js Please Please Friends,如果可以的话,请告诉我我在这里缺少什么或上面的代码有什么问题。我也曾尝试通过谷歌搜索自行解决此问题,但没有找到解决方法

0 个答案:

没有答案