Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MUI自动完成不';不要以钩形工作_Javascript_Reactjs_Autocomplete_Material Ui - Fatal编程技术网

Javascript MUI自动完成不';不要以钩形工作

Javascript MUI自动完成不';不要以钩形工作,javascript,reactjs,autocomplete,material-ui,Javascript,Reactjs,Autocomplete,Material Ui,问题摘要 我在不同的时间会犯不同的错误。当我选择一个建议选项时,我得到以下错误和警告: Material UI:Autocomplete的getOptionLabel方法返回未定义的值,而不是0的字符串 物料界面:提供给自动完成的值无效。没有一个选项与0匹配 另外,该选项不会被选中,输入将变得未定义。但是,当第二次尝试选择某个值时,它会被选中(但仍显示错误) 当我清除输入时,会出现以下错误: A component is changing the controlled value state o

问题摘要

我在不同的时间会犯不同的错误。当我选择一个建议选项时,我得到以下错误和警告:

Material UI:Autocomplete的getOptionLabel方法返回未定义的值,而不是0的字符串

物料界面:提供给自动完成的值无效。没有一个选项与0匹配

另外,该选项不会被选中,输入将变得未定义。但是,当第二次尝试选择某个值时,它会被选中(但仍显示错误)

当我清除输入时,会出现以下错误:

A component is changing the controlled value state of Autocomplete to be uncontrolled.
Elements should not switch from uncontrolled to controlled (or vice versa).
Decide between using a controlled or uncontrolled Autocomplete element for the lifetime of the component.
The nature of the state is determined during the first render, it's considered controlled if the value is not `undefined`.
自动完成组件的代码


有什么问题吗?

以防万一:你必须使用Autocomplete的
defaultValue
而不是控制器

以防万一:你必须使用Autocomplete的
defaultValue
而不是控制器

const AutocompleteUnit = ({control, label, name, ...rest}) => {

  return (
    <>
      <Controller
        onChange={([,data]) => data}
        name={name}
        as={
          <Autocomplete
          {...rest}
          autoHighlight
          style={{marginTop: "25px"}}
          getOptionLabel={option => option.label}
          renderInput={params => (
            <TextField
              {...params}
              label={label}
              variant="outlined"
            />
          )}
        />
        }
        control={control}
        defaultValue={rest.options[0]}
      />
</>

}

const districtOptions = [
    { value: "ciutatvella", label: "Ciutat Vella" },
    { value: "gracia", label: "Gràcia" },
    { value: "eixample", label: "L'Eixample" },
    { value: "sarria", label: "Sarrià" }
  ];