答案 0 :(得分:0)
根据您的代码。在为选择命名时遇到问题。
<Select
key={index}
name="newstatus" {// shoule be diffrent for each tag}
value={this.state[val.name] ? this.state[val.name] : ""}
onChange={this.onChange}
inputProps={{ name: val.name }}
>
答案 1 :(得分:0)
您可以简单地使用对象在状态内的选项来使其正常工作。
this.state = {
options: {
change_status1: "Vacation",
change_status2: "Not Available"
}
};
onChange = e => {
let options = this.state.options;
options[e.target.name] = e.target.value;
this.setState({ options }, () => {
console.log("newState", options);
});
};
loadoptions = () => {
const OptionsArray = [];
let OptionsData = _options;
let stateOptions = this.state.options;
OptionsData.forEach((val, index) => {
let OptionsDatavalue = val.value;
let selectName = val.name.split(" ").join("_").toLowerCase();
OptionsArray.push(
<div key={index}>
<label>Select {val.name} </label>
<select
key={index}
name={selectName}
defaultValue={
stateOptions[selectName] ? stateOptions[selectName] : ""
}
onChange={this.onChange}
>
{OptionsDatavalue.map(option => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
</div>
);
});
return OptionsArray;
};
render() {
return <div className="App">{this.loadoptions()}</div>;
}
这是小提琴。