[英]React - Mui - Autocomplete - asks for keys
console: Warning: Encountered two children with the same key控制台:警告:遇到两个孩子使用相同的密钥
how can i add keys?我怎样才能添加密钥?
i tried我试过
<Stack spacing={2} sx={{ width: 300 }}>
<Autocomplete
id="free-solo-demo"
freeSolo
options={cityObj.map((option) => option.name)}
renderInput={(params) => (
<TextField
{...params}
error={weatherData?.error ? true : false}
fullWidth
label="name your city here..."
value={search}
onChange={(e) => {
setSearch(e.target.value);
}}
ref={inputField}
onKeyDown={(e) => e.key === "Enter" && handleSubmit(e)}
/>
)}
/>
</Stack>
Autocomplete
automatically maps your options and put the option value as key for each option. Autocomplete
会自动映射您的选项并将选项值作为每个选项的键。 So evidently you have some duplicate name
values in in your cityObj
array.很明显,您的cityObj
数组中有一些重复的name
值。
I don't know if there is a way to change the keys in order to suppress the warning, but even if there is, I recommend to not fiddling with it and fixing the problem in a correct manner by removing the duplicate value from your options array .我不知道是否有办法更改键以抑制警告,但即使有,我建议不要摆弄它并通过从选项中删除重复值以正确的方式解决问题阵列。 Because why would we have two or more identical options in a autocomplete anyway?因为为什么我们会在自动完成中有两个或更多相同的选项呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.