gpt4 book ai didi

javascript - 未捕获的 TypeError : this. onSubmit 不是 React.js 中的函数

转载 作者:行者123 更新时间:2023-12-03 01:41:52 24 4
gpt4 key购买 nike

我正在尝试调用 Open Source weather API使用 react 。

但我收到以下错误 App.js:59 Uncaught TypeError: this.onSubmit is not a function

这是我的 API 文件:

import axios from 'axios';

var key = 'TK421';
var countryCode = 'us';

export const getWeatherByZip = zipCode => {
return axios
.get(`http://api.openweathermap.org/data/2.5/weather?zip=${zipCode},${countryCode}&appid=${key}`)
.then(resp => resp.data);
};

这是我的主/应用程序文件:

import React, { Component } from 'react';
import * as api from '../utils/api';

import '../scss/app.scss';

export default class App extends Component {
constructor(props) {
super(props);
this.state = {
zipcode: '',
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(event) {
var zip = event.target.value;
this.setState(function() {
return {
zipcode: zip,
};
});
}

handleSubmit(event) {
event.preventDefault();
this.onSubmit(
api.getWeatherByZip(this.state.zipcode).then(
function(zip) {
this.setState(function() {
return {
zipcode: zip,
};
});
}.bind(this)
)
);
}

render() {
return (
<div className="container">
<form onSubmit={this.handleSubmit.bind(this)}>
<h2>Open Weather App</h2>
<div className="row">
<div className="one-half column">
<label htmlFor="insertMode">Insert your location</label>
<input
className="u-full-width"
placeholder="please enter your zipcode"
type="text"
autoComplete="off"
value={this.state.zipcode}
onChange={this.handleChange.bind(this)}
/>
</div>
<div className="one-half column">
<label htmlFor="showMin">show minimum</label>
<input type="checkbox" />
<label htmlFor="showMax">show maximum</label>
<input type="checkbox" />
<label htmlFor="showMean">show mean</label>
<input type="checkbox" />
</div>
</div>
<div className="row">
<div className="two-half column">
<button className="button-primary" type="submit" disabled={!this.state.zipcode}>
Submit
</button>
</div>
</div>
</form>
</div>
);
}
}

在 React 中输入并提交表单时如何获得正确的响应?

更新:

handleSubmit(event) {
event.preventDefault();
api.getWeatherByZip(this.state.zipcode).then(
function(zip) {
console.log('zip', zip);

this.setState(function() {
return {
zipcode: zip,
};
});
}.bind(this)
);
}

在我的 JSX 中:

 <div className="container">
<form
onSubmit={() => {
this.handleSubmit;
}}
>
<h2>Open Weather App</h2>
<div className="row">
<div className="one-half column">
<label htmlFor="insertMode">Insert your location</label>
<input
className="u-full-width"
placeholder="please enter your zipcode"
type="text"
autoComplete="off"
value={this.state.zipcode}
onChange={this.handleChange.bind(this)}
/>
</div>
<div className="one-half column">
<label htmlFor="showMin">show minimum</label>
<input type="checkbox" />
<label htmlFor="showMax">show maximum</label>
<input type="checkbox" />
<label htmlFor="showMean">show mean</label>
<input type="checkbox" />
</div>
</div>
<div className="row">
<div className="two-half column">
<button className="button-primary" type="submit" disabled={!this.state.zipcode}>
Submit
</button>
</div>
</div>
</form>
</div>

最佳答案

您已经将方法handleSubmit绑定(bind)到表单onsubmit事件,您不需要将此方法的内容包装在this.onSubmit。这会失败,因为您的代码中的类中没有定义此方法。

顺便说一句,您在代码中绑定(bind)了 this 上下文两次,一次在 onsubmit 事件中,一次在构造函数中。只需执行一次就足够了,建议将其放置在构造函数中。

关于javascript - 未捕获的 TypeError : this. onSubmit 不是 React.js 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50807235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com