您好我試圖從後端獲取選項列表,然後將它們映射到選項列表並添加到列表,但失敗。任何人都可以請指教嗎?reactjs輸入選擇選項無法添加
父組件:
fetch(urlMakerNames)
.then((response) => response.json())
.then((responseJson) => {
var array = JSON.parse(responseJson.marker_names);
var options = array.map((opt, index) => {
console.log('opt = ' + opt + ', index = ' + index);
return '<option key="' + index + '" value="' + opt + '">' + opt + '</option>';
});
console.log('BEFORE options = ' + options + ', markerNames = ' + this.state.markerNames);
this.setState({
markerNames: options
});
console.log('AFTER options = ' + options + ', markerNames = ' + this.state.markerNames);
}).catch((error) => {
console.error("MarkerForm error = " + error);
});
子組件:
console.log('this.props.markerNames = ' + this.props.markerNames);
<FormGroup>
<Input type="select" name="markerName" onChange={this.props.handleInputChange} disabled={this.props.isViewMode} required>
<option key="12345" value="TEST">TEST</option>
{this.props.markerNames}
</Input>
</FormGroup>
日誌顯示:
opt = zzz, index = 0
BEFORE options = <option key="0" value="zzz">zzz</option>, markerNames =
this.props.markerNames = <option key="0" value="zzz">zzz</option>
AFTER options = <option key="0" value="zzz">zzz</option>, markerNames = <option key="0" value="zzz">zzz</option>
正如從日誌可以看出,markerNames被傳遞到子組件的正確格式與<option key="12345" value="TEST">TEST</option>
匹配,但只有TEST選項可以在輸入選擇ele中看到但是zzz消失了。
FormGroup任何LIB的一部分? –