最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React chip input field - Stack Overflow

programmeradmin4浏览0评论

I'm using React 17 and tried to use different chip input fields like material-ui-chip-input etc. Unfortunately, all npm packages I tried do not work with react version 17.

Do you know of any ponent that works with this version or how I could create one myself?

Thanks in advance

Edit: I have managed to do what I wanted, but unfortunately, I now get the following error when pressing enter / adding a new item:

index.js:1 Warning: Cannot update a ponent (CreatePoll) while rendering a different ponent (ForwardRef(Autoplete))

Here is the codesandbox which shows the problem: =/src/App.tsx

I'm using React 17 and tried to use different chip input fields like material-ui-chip-input etc. Unfortunately, all npm packages I tried do not work with react version 17.

Do you know of any ponent that works with this version or how I could create one myself?

Thanks in advance

Edit: I have managed to do what I wanted, but unfortunately, I now get the following error when pressing enter / adding a new item:

index.js:1 Warning: Cannot update a ponent (CreatePoll) while rendering a different ponent (ForwardRef(Autoplete))

Here is the codesandbox which shows the problem: https://codesandbox.io/s/passionate-greider-wr9wq?file=/src/App.tsx

Share Improve this question edited Jun 2, 2023 at 14:32 NearHuscarl 81.5k22 gold badges318 silver badges280 bronze badges asked Dec 4, 2021 at 12:54 xeraphimxeraphim 4,64511 gold badges61 silver badges116 bronze badges 1
  • I have used this some time back on mui v4, check this lab ponent and mui core – kiranvj Commented Dec 5, 2021 at 6:24
Add a ment  | 

3 Answers 3

Reset to default 10

why don't you use this instead of a new package? Autoplete exactly do that.

const [receivers, setReceivers] = React.useState<string[]>([]);


    import Autoplete from '@mui/material/Autoplete';
         <Autoplete
            multiple
            onChange={(e, value) => setReceivers((state) => value)}
            id="tags-filled"
            options={top100Films.map((option) => option.title)}
            defaultValue={[top100Films[13].title]}
            freeSolo
            renderTags={(value, getTagProps) =>
              value.map((option, index) => (
                <Chip variant="outlined" label={option} {...getTagProps({ index })} />
              ))
            }
            renderInput={(params) => (
              <TextField
                {...params}
                variant="filled"
                label="freeSolo"
                placeholder="Favorites"
              />
            )}
          />
import React from 'react'
import { MuiChipsInput } from 'mui-chips-input'

const MyComponent = () => {
  const [chips, setChips] = React.useState([])

  const handleChange = (newChips) => {
    setChips(newChips)
  }

  return (
    <MuiChipsInput value={chips} onChange={handleChange} />
  )
}

Source: https://github./viclafouch/mui-chips-input

Supports both React 17 / 18 and MUI V5

Use the freeSolo prop of Autoplete, and just set the options prop to an empty array:

 <Autoplete
    multiple
    freeSolo
    value={value}
    options={[]}
    renderTags={(value, getTagProps) =>
      value.map((option, index) => (
        <Chip label={option} {...getTagProps({ index })} onDelete={()=> /* remove option from value array */} />
      ))
    }
    renderInput={params => (
      <TextField
        {...params}
        variant="outlined"
        label="Add Chip"
        placeholder="Type and press enter"
      />
    )}
  />
发布评论

评论列表(0)

  1. 暂无评论