小编Rot*_*nik的帖子

Material ui 自动完成功能无法将选项参数识别为数组

我正在尝试创建一个自动完成字段,该字段从组件状态中获取选项(状态从后端获取它)。这是我的组件:

export const Person: React.FC<PersonProps> = ({name, avatar, setMainState}: PersonProps) => {
  const [location, setLocation] = useState('');
  const [options, setOptions] = useState([]);
  const change = (event: any) => {
    setLocation(event.target.value)
    setMainState(event.target.value)
  }
  
  useEffect(() => {
    axios
      .get(`http://localhost:8080/autocomplete/?str=` + location)
      .then(res => {
        setOptions(res.data);
      })
  },[location])

  return <Box display="flex" height="30%">
    <Typography>{name}</Typography>
    <Autocomplete
      id="combo-box-demo"
      options={options}
      getOptionLabel={(option) => option as string}
      style={{ width: 300 }}
      renderInput={(params) => <TextField {...params} label="Combo box" variant="outlined" />}
    />
  </Box>
};
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,我的状态中的选项数组不被识别为数组(尽管我用空数组初始化了它)。我收到这个警告:

index.js:1 Warning: Failed prop type: Invalid …
Run Code Online (Sandbox Code Playgroud)

javascript autocomplete typescript reactjs material-ui

3
推荐指数
1
解决办法
3772
查看次数