React-Select onChange 存储要传递给 API 调用的值
React-Select onChange to store a value to pass to API call
我正在开发我的第一个网络应用程序并引导这个应用程序,我在更改 API 获取请求的 URL 时遇到问题。我正在使用 react-select 创建一个下拉菜单,我希望下拉菜单 selection 改变搜索条件,例如:
我有一个baseurl.com/
并希望根据下拉列表 selection 创建一个变量,以附加到 baseurl。
我在下拉列表中的两个选项是 'Name' 和 'Birthday',
如果你 select 'Name',URL 将是
baseurl.com/Patient?name= + inputvalue.
如果你 select 'birthday',URL 将是
baseurl.com/Patient?birthdate=eq + inputvalue
我想保持基础URL不变,因为我最终必须向select添加更多选项。我已经在我的应用程序中使用了输入值,所以我相信我不需要对其进行更改。
这是我到目前为止的一些代码,当我制作 selection 时,它给了我一个“无法读取 属性 'value' 未定义的错误”。我也没有' 尚未使组件将状态存储为变量,但当涉及到它时我会过桥 任何见解表示赞赏,谢谢:
const choice = [
{value : "Name", label: "Name" },
{value : "bDay", label: "Birthday (YYYY-MM-DD)"}
];
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
inputValue: 'Search',
queryResult: {},
criteria: '',
showSVG: false
};
this.handleChange = this.handleChange.bind(this);
this.getInputValue = this.getInputValue.bind(this);
this.baseURL = this.baseURL.bind(this);
}
getInputValue(e) {
this.setState({ inputValue: e.target.value });
}
handleChange(e) {
this.setState({criteria: e.target.value});
console.log(e.target.value);
}
baseURL(e) {
const url = 'https://baseURL.com/';
[BLOCK FOR FETCH REQUEST]
render() {
return (
<div className="input-wrapper">
{/* ON CHANGE SELECT */}
<Select options={choice} value={this.state.criteria} onChange={this.handleChange} />
<input
type="text"
value= {this.state.inputValue}
onFocus = {() => this.setState({ inputValue: '' })}
onChange={this.getInputValue}
onKeyDown={this.baseURL} />
<img className={this.state.showSVG ? "isVisable" : ""} src="assets/icons/grid.svg" />
{ Object.keys(this.state.queryResult).length !== 0 ? <PatientInfoBlock data={this.state.queryResult} /> : null }
{ !this.state.queryResult ? <h3>Sorry no results match ''{this.state.inputValue}''</h3> : null }
</div>
);
}
'''
handleChange 应该是:
handleChange(selectedOption) {
this.setState({criteria: selectedOption});
}
选择的选项类型是:
{
value: string,
label: string
}
明白了,感谢 armin yahya 为我指明了正确的方向,因为我不知道 selectedOption 类型。这是我最终写的,它使我的 handleChange 函数正常工作并为 API 调用更新了 URL:
handleChange(selectedOption) {
if(selectedOption.value == 'Name') {
this.setState({criteria: 'Patient?name='});
}
if(selectedOption.value == 'bDay') {
this.setState({criteria: 'Patient?birthdate=eq'});
}
我正在开发我的第一个网络应用程序并引导这个应用程序,我在更改 API 获取请求的 URL 时遇到问题。我正在使用 react-select 创建一个下拉菜单,我希望下拉菜单 selection 改变搜索条件,例如:
我有一个baseurl.com/
并希望根据下拉列表 selection 创建一个变量,以附加到 baseurl。 我在下拉列表中的两个选项是 'Name' 和 'Birthday', 如果你 select 'Name',URL 将是 baseurl.com/Patient?name= + inputvalue.
如果你 select 'birthday',URL 将是 baseurl.com/Patient?birthdate=eq + inputvalue
我想保持基础URL不变,因为我最终必须向select添加更多选项。我已经在我的应用程序中使用了输入值,所以我相信我不需要对其进行更改。
这是我到目前为止的一些代码,当我制作 selection 时,它给了我一个“无法读取 属性 'value' 未定义的错误”。我也没有' 尚未使组件将状态存储为变量,但当涉及到它时我会过桥 任何见解表示赞赏,谢谢:
const choice = [
{value : "Name", label: "Name" },
{value : "bDay", label: "Birthday (YYYY-MM-DD)"}
];
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
inputValue: 'Search',
queryResult: {},
criteria: '',
showSVG: false
};
this.handleChange = this.handleChange.bind(this);
this.getInputValue = this.getInputValue.bind(this);
this.baseURL = this.baseURL.bind(this);
}
getInputValue(e) {
this.setState({ inputValue: e.target.value });
}
handleChange(e) {
this.setState({criteria: e.target.value});
console.log(e.target.value);
}
baseURL(e) {
const url = 'https://baseURL.com/';
[BLOCK FOR FETCH REQUEST]
render() {
return (
<div className="input-wrapper">
{/* ON CHANGE SELECT */}
<Select options={choice} value={this.state.criteria} onChange={this.handleChange} />
<input
type="text"
value= {this.state.inputValue}
onFocus = {() => this.setState({ inputValue: '' })}
onChange={this.getInputValue}
onKeyDown={this.baseURL} />
<img className={this.state.showSVG ? "isVisable" : ""} src="assets/icons/grid.svg" />
{ Object.keys(this.state.queryResult).length !== 0 ? <PatientInfoBlock data={this.state.queryResult} /> : null }
{ !this.state.queryResult ? <h3>Sorry no results match ''{this.state.inputValue}''</h3> : null }
</div>
);
}
'''
handleChange 应该是:
handleChange(selectedOption) {
this.setState({criteria: selectedOption});
}
选择的选项类型是:
{
value: string,
label: string
}
明白了,感谢 armin yahya 为我指明了正确的方向,因为我不知道 selectedOption 类型。这是我最终写的,它使我的 handleChange 函数正常工作并为 API 调用更新了 URL:
handleChange(selectedOption) {
if(selectedOption.value == 'Name') {
this.setState({criteria: 'Patient?name='});
}
if(selectedOption.value == 'bDay') {
this.setState({criteria: 'Patient?birthdate=eq'});
}