[英]React Context API / Bind function in context file?
I am trying to use the context api. 我正在尝试使用上下文api。 I'm getting an error TypeError: 'this.props.fetchWeather is not a function' when I submit the form.
提交表单时出现错误TypeError:'this.props.fetchWeather不是函数'。
My WeatherContext file has a WeatherProvider & WeatherConsumer. 我的WeatherContext文件具有WeatherProvider和WeatherConsumer。 My state and a fetchWeather function are this file.
我的状态和fetchWeather函数是此文件。 When I log what is being passed to the value fetchWeather logs as undefined.
当我记录什么时,传递给值fetchWeather的日志是未定义的。
{term: "", forecast: Array(0), fetchWeather: undefined}
My question is how do I define the fetchWeather function within the WeatherContext? 我的问题是如何在WeatherContext中定义fetchWeather函数? I tried binding it.
我尝试绑定它。 I'm not sure where I'm going wrong.
我不确定我要去哪里。
This question has been updated to reflect changes I made ... 此问题已更新,以反映我所做的更改...
src/index.js src / index.js
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import { WeatherProvider, WeatherContext } from
'./context/WeatherContext'
ReactDOM.render(
<WeatherProvider>
<WeatherContext.Consumer>
{({ term, forecast, fetchWeather }) =>
<App term={ term }
forecast={ forecast }
fetchWeather={ fetchWeather } />
}
</WeatherContext.Consumer>
</WeatherProvider>,
document.getElementById('root')
);
registerServiceWorker();
src/app.js src / app.js
import React, { Component } from 'react'
import SearchForm from './components/Search/Search_Form'
export default class App extends React.Component {
render() {
return (
<div className="App">
<SearchForm {...this.props}/>
</div>
);
}
}
src/context/WeatherContext src / context / WeatherContext
import React, { Component } from 'react'
import axios from 'axios'
import _ from 'lodash'
export const WeatherContext = React.createContext();
export class WeatherProvider extends React.Component {
state = {
context: {
input: '',
forecast: [],
fetchWeather: this.fetchWeather
}
}
fetchWeather = (term) => {
let QUERY = term
const KEY = `key`
let URL = `http://url.com/q=${QUERY}&appid=${KEY}`
axios.get(URL)
.then( res => {
// do something ...
})
}
render() {
return (
<WeatherContext.Provider
value={{ ...this.state.context }} >
{ this.props.children }
</WeatherContext.Provider>
)
}
}
src/components/Search/index src /组件/搜索/索引
import React from 'react'
import { WeatherContext } from '../../context/WeatherContext'
import SearchForm from './Search_Form'
export default (props) => (
<WeatherContext.Consumer>
{
({fetchWeather}) =>
<SearchForm {...props} fetchWeather={fetchWeather} />
}
</WeatherContext.Consumer>
)
src/components/Search/Search_Form src / components / Search / Search_Form
import React, { Component } from 'react'
class SearchForm extends Component {
handleFormSubmit = (e) => {
e.preventDefault();
let term = this.input.value
this.props.fetchWeather(term)
}
handleClear =(e) => {
e.preventDefault();
this.setState({
input: ''
})
}
render() {
console.log(this.props);
return (
<div>
<form className="form" onSubmit={ this.handleFormSubmit }>
<input type='text'
placeholder='Enter a US City'
ref={input => { this.input = input }}/>
<button type="submit"> Search </button>
<button onClick={ this.handleClear }> x </button>
</form>
</div>
)
}
}
export default SearchForm;
From the App component, you are not passing the props to the SearchForm component and since you have wrapped the SearchForm with With WeatherContext
in components/Search/index.js
which you aren't using in App.js, the fetchWeather prop SearchForm is undefined. 从App组件中,您没有将道具传递给SearchForm组件,并且因为您已将SearchForm与With
WeatherContext
一起包装在WeatherContext
中未使用的components/Search/index.js
中,所以fetchWeather道具SearchForm是未定义的。 You need to use SearchForm in App.js from components/Search/index.js
like 您需要在App.js中使用
components/Search/index.js
类的SearchForm
import React, { Component } from 'react'
import SearchForm from './components/Search/index.js'
export default class App extends React.Component {
render() {
return (
<div className="App">
<SearchForm />
</div>
);
}
}
or 要么
pass the props down from App.js 从App.js传递道具
import React, { Component } from 'react'
import SearchForm from './components/Search/Search_Form'
export default class App extends React.Component {
render() {
return (
<div className="App">
<SearchForm {...this.props}/>
</div>
);
}
}
Resolved the undefined fetchWeather issue. 解决了未定义的fetchWeather问题。 The answer was to take the fetchWeather function out of the state and put it inside a constructor in the WeatherProvider class.
答案是从状态中取出fetchWeather函数,并将其放在WeatherProvider类的构造函数中。 Then bind it.
然后将其绑定。 I was tired...
我很累
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.