[英]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.