Material-ui lt;Autocomplete /gt; getOptionLabel - passing empty string as value(Material-UIlt;AutoComplete/gt;getOptionLabel-将空字符串作为值传递)
本文介绍了Material-UI<;AutoComplete/>;getOptionLabel-将空字符串作为值传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是Material-UI AutoComplete。我将一些状态数组传递给它的属性选项。我面临的问题是getOptionLabel:
Material-UI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string for [""].
我有两个组件。子项为:
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" />
)}
/>
);
};
在父组件中,我调用此子组件:
<StateSelect
state={selectedState}
setState={(state) => setSelectedState(state)}
/>
在父对象中,我有一个控制StateSelect:
值的Reaction钩子 const [selectedState, setSelectedState] = useState([""]);
所以,当我最初将selectedState作为属性传递给StateSelect时,它是[‘’],并且我收到了这个错误消息。如何将空值作为初始值传递而不出现此错误?
我上传了代码的简单版本:
https://codesandbox.io/s/smoosh-field-j2o1p?file=/src/inputStates/input.js
推荐答案
我收到了相同的错误。而且,下面的方法对我很有效。
getOptionLabel={(option) => option.name || ""}
这篇关于Material-UI<;AutoComplete/>;getOptionLabel-将空字符串作为值传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Material-UI<;AutoComplete/>;getOptionLabel-将空字符串作为值传递


猜你喜欢
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01