简体   繁体   English

Material-ui<autocomplete /> getOptionLabel - 将空字符串作为值传递

[英]Material-ui <Autocomplete /> getOptionLabel - passing empty string as value

I am using material-ui autocomplete.我正在使用 material-ui 自动完成。 I am passing to its property options some array of states.我将一些状态数组传递给它的属性选项。 The problem I face is with getOptionLabel:我面临的问题是getOptionLabel:

 Material-UI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string for [""].

I have 2 components.我有 2 个组件。 The child one is:孩子一是:

const StateSelect = (props) => {
  const classes = useStyles();
  const handlePick = (e, v) => {
    props.setState(v);
  };
  return (
    <Autocomplete
      className={classes.inputStyle}
      options={states}
      getOptionLabel={(option) => (option ? option.name : "")}
      onChange={handlePick}
      value={props.state}
      renderInput={(params) => (
        <TextField {...params} label="State" variant="outlined" />
      )}
    />
  );
};

And in the parent one I invoke this child component:在父组件中,我调用了这个子组件:

 <StateSelect
            state={selectedState}
            setState={(state) => setSelectedState(state)}
          />

In the parent one I have the React hook that controls value of the StateSelect:在父项中,我有控制 StateSelect 值的 React 钩子:

  const [selectedState, setSelectedState] = useState([""]);

So when I initially pass selectedState as prop to StateSelect it is [''] and I am getting this error message.因此,当我最初将 selectedState 作为道具传递给 StateSelect 时,它是 [''] 并且我收到此错误消息。 How could I pass empty value as initial and not to get this error?如何将空值作为初始值传递而不出现此错误?

I uploaded simple version of my code:我上传了我的代码的简单版本:

https://codesandbox.io/s/smoosh-field-j2o1p?file=/src/inputStates/input.js https://codesandbox.io/s/smoosh-field-j2o1p?file=/src/inputStates/input.js

Your default value is an array with an empty string [""] which when evaluated in the ternary will return true, thus trying to access the property option.name that is undefined.您的默认值是一个带有空字符串[""]的数组,在三元组中评估时将返回 true,因此尝试访问未定义的属性option.name You need either to test for that or initialise the state with an empty string which is a falsy value in JS.您需要对此进行测试或使用空字符串初始化 state,该空字符串在 JS 中是一个虚假值。

I got the same error.我得到了同样的错误。 And, the following worked for me.而且,以下对我有用。

getOptionLabel={(option) => option.name || ""}

I faced a similar problem and I had options as an array of strings.我遇到了类似的问题,我可以选择字符串数组。 Eg: ["delhi", "new york", "mumbai"] .例如: ["delhi", "new york", "mumbai"] I fixed it by adding a check, if the option is a string, if not use an empty string.我通过添加检查来修复它,如果选项是字符串,如果不是使用空字符串。

getOptionLabel={(option) => typeof option === 'string'
                  || option instanceof String ? option : ""}

I solved this problem by just setting values like this.我通过设置这样的值解决了这个问题。

getOptionLabel={(option)=>(option.name?option.name:'')}

value={props.state || null} value={props.state || null} this worked for me in material UI version 4 value={props.state || null}这在材料 UI 版本 4 中对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM