gpt4 book ai didi

javascript - 条件API调用并在react-select中发送数据

转载 作者:行者123 更新时间:2023-12-01 03:19:43 25 4
gpt4 key购买 nike

import React, { Component} from 'react';  
import Select from 'react-select';
import 'react-select/dist/react-select.css';

const partsType = [
{value: 'front_parts', label: 'Part Condition-Front'},
{value: 'left_parts', label: 'Part Condition-Left'},
{value: 'back_parts', label: 'Part Condition-Back'},
{value: 'right_parts', label: 'Part Condition-Right'},
{value: 'top_bottom_parts', label: 'Part Condition-Top/Bottom'},
{value: 'glass', label: 'Glass Condition'},
{value: 'electrical_parts', label: 'Electrical Parts'},
{value: 'non_electrical_parts', label: 'Non-Electrical Parts'}
];

const getParts = () => {
return fetch(
"http://localhost:4000/left_parts",
{
method: 'get'
}
)
.then(response => {
if(response.status >= 400) {
throw new Error("error");
}
return response.json()
})
.then(parts => {
let partsName = [];
for(let part of parts) {
partsName.push({value: part.promptCode, label: part.text})
}
return {options: partsName};
})
.catch(err => {
console.log('could not fetch parts');
console.log(err);
return {options: []}
})
};

class Assess extends Component {

constructor(props) {
super(props);
this.state = {
partsType:'front_parts'
};

this.handlePartsType = this.handlePartsType.bind(this);

handlePartsType = (item) => {
this.setState({
partsType: item.value
});
};

render() {
return (
<div>
<Select
clearable={false}
searchable={false}
value={this.state.partsType}
options={partsType}
onChange={this.handlePartsType}
/>

<Select.Async
clearable={false}
searchable={false}
name="PartNamePolygon"
value={this.state.PartNamePolygon}
onChange={this.PartNamePolygonSelect}
loadOptions={getParts}
/>
</div>
);
}
}

我已经提供了片段。我现在正在做的是我做了两个下拉菜单,并且使用第一个下拉菜单的第二个下拉菜单的数据将被更改。现在我不知道如何根据 this.state.partsType 调用不同的 API,因为根据其状态值,其值将在“getParts”中传递。如何实现这一目标?将其值传递给它,以便调用不同的 API

最佳答案

尝试这样

import React, { Component} from 'react';  
import Select from 'react-select';
import 'react-select/dist/react-select.css';

const partsType = [
{value: 'front_parts', label: 'Part Condition-Front'},
{value: 'left_parts', label: 'Part Condition-Left'},
{value: 'back_parts', label: 'Part Condition-Back'},
{value: 'right_parts', label: 'Part Condition-Right'},
{value: 'top_bottom_parts', label: 'Part Condition-Top/Bottom'},
{value: 'glass', label: 'Glass Condition'},
{value: 'electrical_parts', label: 'Electrical Parts'},
{value: 'non_electrical_parts', label: 'Non-Electrical Parts'}
];

const getParts = (type) => {
return fetch(
`http://localhost:4000/${type}`,
{
method: 'get'
}
)
.then(response => {
if(response.status >= 400){
throw new Error("error");
}
return response.json()
})
.then(parts => {
let partsName = [];


for(let part of parts) {
partsName.push({value: part.promptCode, label: part.text})
}


return {options: partsName};
})
.catch(err => {
console.log('could not fetch parts');
console.log(err);
return {options: []}
})

};

class Assess extends Component {

constructor(props){
super(props);
this.state = {
partsType:'front_parts'

};

this.handlePartsType = this.handlePartsType.bind(this);

handlePartsType = (item) => {
this.setState({
partsType: item.value
}, getParts(item.value));

};

render() {

return (
<div>
<Select
clearable={false}
searchable={false}
value={this.state.partsType}
options={partsType}
onChange={this.handlePartsType}
/>

<Select.Async
clearable={false}
searchable={false}
name="PartNamePolygon"
value={this.state.PartNamePolygon}
onChange={this.PartNamePolygonSelect}
loadOptions={getParts}
/>

</div>
);
}
}

关于javascript - 条件API调用并在react-select中发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45294748/

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