Reactjs 从提取结果自动完成物料
有人能帮我吗?我有一个空材质的自动完成,我是这样设置的:Reactjs 从提取结果自动完成物料,reactjs,autocomplete,material-ui,react-state,Reactjs,Autocomplete,Material Ui,React State,有人能帮我吗?我有一个空材质的自动完成,我是这样设置的: <Autocomplete id="standard-secondary" options={MuscleCategory} getOptionLabel={(option) => option.title} value={muscleGroup} renderInput={(params) => <TextField {...
<Autocomplete id="standard-secondary" options={MuscleCategory} getOptionLabel={(option) => option.title} value={muscleGroup}
renderInput={(params) => <TextField {...params} label={t('muscolarGroupFieldLabel')} variant="outlined" />}
onChange={(val) => {
setMuscleGroup(val.target.value);
}}
style={{ width: 300 }}
第一个用于保存所选项目,第二个用于“显示”自动完成中的所有类别。
我不知道如何填充需要在自动完成中显示的MuscleCategory,我尝试在函数中使用setMuscleGroup:
function populateMuscleCombo(){
fetch(something, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
})
}).then((response) => response.json())
.then((responseJson) => {
setMuscleCategory(responseJson);
}).catch((error) => {
console.error(error);
});
}
但是我什么也没做,PHP是正确的。
const[MuscleCategory,setMuscleCategory]=useState(new Array())
是初始化数组的错误方法,但没有错误,请参阅,因为我可以阅读代码,您正在将整个响应对象传递到AutoComplete options prop
请检查需要传递的数据结构以自动完成它可能需要字符串数组,但是您可能正在传递从后端接收的整个对象数组,您可能需要提取需要传递的内容。请分享AutoComplete
的响应和材料UI API,使用codesandbox发送示例,如果没有完整的代码或摘要,仅通过查看代码是很难理解的
function populateMuscleCombo(){
fetch(something, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
})
}).then((response) => response.json())
.then((responseJson) => {
setMuscleCategory(responseJson);
}).catch((error) => {
console.error(error);
});
}