使用React Quill的Usestate和Onchange无法正常工作,不确定如何修复

发布于 2025-02-11 23:25:21 字数 1064 浏览 4 评论 0原文

我有一系列文本输入字段 - 它们的输入由这样的usestate管理:

const [formFields, setFormFields] = useState([
{ textinput1: "", textinput2: "", textinput3: "", reactQuill: "" } ]);

他们的句柄更改是通过此功能管理的,因为可以添加更多的文本字段,并且添加的每个索引都必须保存其输入:

function handleChangeInput(index, event) {
    const values = [...formFields];
    values[index][event.target.name] = event.target.value
    setFormFields(values);
}

并且这就是他们的外观和功能,如预期的:

{
      formFields.map((formField, index) => (

      <React.Fragement key={index}>

      <TextField

      name='textinput1'
      value={formField.textinput1}
      onChange={event => handleChangeInput(index, event)}
      
      />
      ))
      }

但是,当我尝试使用React Quill(Rich Text Editor)输入的相同的确切逻辑时,它根本无法正常工作。因此,例如,这是行不通的:

 <ReactQuill 
    
    name="reactQuill"
    onChange={event => handleChangeInput(index, event)}
    value={formField.reactQuill}
    

    />

我会遇到“名称”的错误,而我不确定如何解决这个问题?预先感谢您是否知道如何解决此问题!

I have a series of text input fields - their input is managed by useState like this:

const [formFields, setFormFields] = useState([
{ textinput1: "", textinput2: "", textinput3: "", reactQuill: "" } ]);

Their handle change is managed with this function, because more text fields can be added, and every index of the additions has to have its input get saved:

function handleChangeInput(index, event) {
    const values = [...formFields];
    values[index][event.target.name] = event.target.value
    setFormFields(values);
}

And this is what they look like and function as intended:

{
      formFields.map((formField, index) => (

      <React.Fragement key={index}>

      <TextField

      name='textinput1'
      value={formField.textinput1}
      onChange={event => handleChangeInput(index, event)}
      
      />
      ))
      }

However, when I try the same exact logic with the React Quill (rich text editor) input, it's not working at all. So for example, this will not work:

 <ReactQuill 
    
    name="reactQuill"
    onChange={event => handleChangeInput(index, event)}
    value={formField.reactQuill}
    

    />

I am getting an error of "name" being undefined, and I am not sure how to work around this? Thanks in advance if anyone knows how to fix this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一刻暧昧 2025-02-18 23:25:21

问题是固定的。由于React Quill Text Editor不会像其他文本字段那样返回传统事件,因此必须将我的HandleChangeInput修改为:

function handleChangeInput(index, targetName, targetValue) {
     const values = [...formFields];
     values[index][targetName] = targetValue
     setFormFields(values); }

并且必须更改文本字段的OnChange::

onChange={event => handleChangeInput(index, event.target.name, event.target.value)}

React for React Quill必须更改为:

onChange={value => handleChangeInput(index, 'reactQuill', value)}

我希望将来对某人有所帮助!

The issue was fixed. Since the React Quill text editor does not return a traditional event like other text fields do, my handleChangeInput had to be modified to:

function handleChangeInput(index, targetName, targetValue) {
     const values = [...formFields];
     values[index][targetName] = targetValue
     setFormFields(values); }

And the onChange for text fields had to be changed to:

onChange={event => handleChangeInput(index, event.target.name, event.target.value)}

The onChange for React Quill had to be changed to:

onChange={value => handleChangeInput(index, 'reactQuill', value)}

I hope this helps someone in the future!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文