简体   繁体   English

TypeError: dispatch is not a function when using react-context api

[英]TypeError: dispatch is not a function when using react-context api

I am very new to react and currently using Context API, I am trying to get the country name from user input in the form then send back to the context, and in context I am using Componentdidmount to call API and show data, when user input, its saves data alert it but then suddenly shows up with that error.我对反应非常陌生,目前正在使用上下文 API,我试图从表单中的用户输入中获取国家/地区名称,然后发送回上下文,在上下文中我使用 Componentdidmount 调用 API 并在用户输入时显示数据,它保存数据提醒它,但随后突然出现该错误。

This is my form file..这是我的表格文件..

import React, { useState } from 'react'
import { Consumer } from '../../context';
import Spinner from '../spinner';

function Country() {
    const [name, setName] = useState('');

    // 
    function Submit (e, dispatch){
        e.preventDefault();
        alert(`this form is submited ${name}`)

        dispatch({type: 'SELECT_COUNTRY', payload: name});

        setName('');
    }

    return (
        <Consumer>
            { value =>{
                if (!value.chart.length){
                    return <Spinner/>
                }
                else{
                    // setCountry(value.countries)
                    const { dispatch } = value;
                    console.log('coming from dispatch',dispatch)
                    return (
                        <div className='columns'>
                            <div className='column'>
                                <form onSubmit={Submit.bind(dispatch)}>
                                    <div className="field">
                                        <label className="label">Pick your Country</label>
                                        <div className="control has-icons-left has-icons-right">
                                            <input className="input" type="text" placeholder="Enter Country name..." value={name} onChange={e => setName(e.target.value)}/>
                                            <span className="icon is-small is-left">
                                                <i className="fas fa-globe-asia"></i>
                                            </span>
                                            <span className="icon is-small is-right">
                                                <i className="fas fa-check"></i>
                                            </span>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    );**strong text**
                }
            }}
        </Consumer>
    )
}


export default Country;

This is my Context file..这是我的上下文文件..

import React, { Component } from 'react';
import axios from "axios";
import Country from './components/country/country';

const Context = React.createContext();

const reducer = (state, action) => {
    switch(action.type) {
        case 'SELECT_COUNTRY':
            return {
                ...state,
                cont:action.payload
            };
        default:
            return state;
    }
}

export class Provider extends Component {

    state = {
        data : {},
        chart : {},
        countries : {},
        cont: '',
        dispatch : action => this.setState(state =>
            reducer(state,action))
    }
    componentDidMount(){
        axios.get('https://covid19.mathdro.id/api')
            .then(res => {
                // console.log(res.data)
                this.setState({ data : res.data});
            })
            .catch(err => console.log(err))

        axios.get('https://covid19.mathdro.id/api/daily')
        .then(res => {
            const result = res.data.map(({ confirmed, deaths, reportDate: date }) => ({ confirmed: confirmed.total, deaths: deaths.total, date }));
            this.setState({ chart : result});
        })
        .catch(err => console.log(err))

        axios.get('https://covid19.mathdro.id/api/countries')
        .then(res => {
            console.log('yeh country ka res h', res.data.countries)
            const { countries } = res.data;
            // console.log('yesh country ka destructuring h',countries)

            this.setState({ countries : countries.map( country => country.name)});
        })
        .catch(err => console.log(err))

    }

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

export const Consumer = Context.Consumer;

You were not calling your Submit function correctly, try this:您没有正确调用您的 Submit function,试试这个:

form onSubmit={(e)=>Submit(e,dispatch)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用来自 react context-api 的调度时获取 typeError - getting typeError while using dispatch from react context-api 反应上下文 API - 调度不是 function - React Context API - dispatch is not a function 使用 React-Context 时对象不可迭代错误 - Object is not iterable Error while Using React-Context 反应:TypeError:调度不是 function - React: TypeError: dispatch is not a function 如何修复:React-Context - TypeError: Object 不可迭代(无法读取属性 Symbol(Symbol.iterator)) - How to fix: React-Context - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) React TypeError: Object(...)(...) is undefined when using Context API - React TypeError: Object(...)(...) is undefined when using Context API 面临类型错误:使用上下文 API 时,渲染不是 function - Faced TypeError: render is not a function when using Context API 使用 useReducer/useContext 和 React-Testing-Library 时出现“TypeError: dispatch is not a function” - "TypeError: dispatch is not a function" when using useReducer/useContext and React-Testing-Library TypeError:dispatch 不是 React 和 Redux 的函数 - TypeError: dispatch is not a function React and Redux React context api dispatch 在调用时不调度值 - React context api dispatch does not dispatch values when called when called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM