react使用mui里的Autocomplete组件报错问题

文章讲述了在使用Autocomplete组件时,如何修正因将带有key属性的对象直接用在JSX中的警告,并提供了正确的代码示例,包括使用`renderOption`和`renderTags`来处理选项渲染。
摘要由CSDN通过智能技术生成
Autocomplete报错Warning: A props object containing a "key" prop is being spread into JSX: let props = {key: someKey, tabIndex: ..., role: ..., id: ..., onMouseMove: ..., onClick: ..., onTouchStart: ..., data-option-index: ..., aria-disabled: ..., aria-selected: ..., className: ..., children: ...}; <li {...props} /> React keys must be passed directly to JSX without using spread: let props = {tabIndex: ..., role: ..., id: ..., onMouseMove: ..., onClick: ..., onTouchStart: ..., data-option-index: ..., aria-disabled: ..., aria-selected: ..., className: ..., children: ...}; <li key={someKey} {...props} />

解决方法:

原代码

<Autocomplete
              disablePortal
              id="combo-box-demo"
              options={keyword}
              filterOptions={function (options: { label: string; key: string; title: string; }[]): { label: string; key: string; title: string; }[] {
                const mate = keyword.filter((suggestion) =>
                  suggestion.title.toLowerCase().includes(inputValue)
                );
                return mate
              }}
              getOptionLabel={function (mate) {
                return mate.label
              }}
              sx={{ width: 580, background: "#f8fafb", borderRadius: '8px' }}
              renderInput={(params) => (
                <TextField
                  {...params}
                  placeholder="欢迎使用服务支持"
                  onFocus={clearPlaceholderText}
                  onBlur={restorePlaceholderText}
                  InputProps={{
                    ...params.InputProps,
                    endAdornment: (
                      <InputAdornment position="end">
                        <IconButton className="iconBtn">
                          <SearchIcon />
                        </IconButton>
                      </InputAdornment>
                    ),
                  }}
                />
              )}
              onChange={(event, value) => handleSearch(value)}
              noOptionsText={<CustomNoOptions />}
              open={open}
              onClose={() => setOpen(false)}
              onInputChange={handleInputChange}
              value={selectedOption}
            />

加入这些即可

import Chip from '@mui/material/Chip';

renderOption={(props, option) => {
                const key: any = option.key;
                const { ...otherProps } = props;
                return (
                  <li {...otherProps} key={key}>
                    {option.label}
                  </li>
                );
              }}
              renderTags={(tagValue, getTagProps) => {
                return tagValue.map((option, index) => (
                  <Chip {...getTagProps({ index })} key={option.key} label={option.label} />
                ))
              }}

最终代码

<Autocomplete
              disablePortal
              id="combo-box-demo"
              options={keyword}
              filterOptions={function (options: { label: string; key: string; title: string; }[]): { label: string; key: string; title: string; }[] {
                const mate = keyword.filter((suggestion) =>
                  suggestion.title.toLowerCase().includes(inputValue)
                );
                return mate
              }}
              getOptionLabel={function (mate) {
                return mate.label
              }}
              sx={{ width: 580, background: "#f8fafb", borderRadius: '8px' }}
              renderInput={(params) => (
                <TextField
                  {...params}
                  placeholder="欢迎使用服务支持"
                  onFocus={clearPlaceholderText}
                  onBlur={restorePlaceholderText}
                  InputProps={{
                    ...params.InputProps,
                    endAdornment: (
                      <InputAdornment position="end">
                        <IconButton className="iconBtn">
                          <SearchIcon />
                        </IconButton>
                      </InputAdornment>
                    ),
                  }}
                />
              )}
              onChange={(event, value) => handleSearch(value)}
              noOptionsText={<CustomNoOptions />}
              open={open}
              onClose={() => setOpen(false)}
              onInputChange={handleInputChange}
              value={selectedOption}
              renderOption={(props, option) => {
                const key: any = option.key;
                const { ...otherProps } = props;
                return (
                  <li {...otherProps} key={key}>
                    {option.label}
                  </li>
                );
              }}
              renderTags={(tagValue, getTagProps) => {
                return tagValue.map((option, index) => (
                  <Chip {...getTagProps({ index })} key={option.key} label={option.label} />
                ))
              }}
            />

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值