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