gpt4 book ai didi

javascript - react 将 onChange 事件传递给子组件

转载 作者:行者123 更新时间:2023-11-30 11:49:28 25 4
gpt4 key购买 nike

我有以下内容:

import React from 'react';
import {render} from 'react-dom';

import Form from './form.jsx';

import axios from 'axios';

class App extends React.Component {
constructor(props) {
super(props);
this.state = {
location: ''
};
}
getSearchedValue(e) {
this.setState({ location: e.target.value });
}
searchNearPlaces (e) {
e.preventDefault();

console.log(this.getSearchedValue());

const clientID = 'xxxxxxxxxxx',
clientSecret = 'xxxxxxxxxxxxx',
version = 'v=20140806';
let location = this.state.location,
url = 'https://api.foursquare.com/v2/venues/explore?client_id=' + clientID + '&client_secret=' + clientSecret + '&near=' + location + '&' + version;

axios.get(url)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
render () {
return (
<section className="main">
<Form action={this.searchNearPlaces.bind(this)} value={this.getSearchedValue.bind(this)} />
</section>
);
}
}

render(<App/>, document.getElementById('app'));

和一个表单组件:

import React from 'react';

class Form extends React.Component {
render () {
return (
<form className="form-group">
<input type="text" onChange={this.props.value} />
<input onClick={this.props.action} type="submit" />
</form>
);
}
}

export default Form;

例如,当我在输入“伦敦”后点击提交时,出现以下错误:

Uncaught TypeError: Cannot read property 'target' of undefined

引用:

  getSearchedValue(e) {
this.setState({ location: e.target.value });
}

最佳答案

可能是因为您代码中的这一行:

console.log(this.getSearchedValue());

因为你没有传递任何东西给它,e 是未定义的。

关于javascript - react 将 onChange 事件传递给子组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39933709/

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