【问题标题】:How to pass multiple arguments to an Inputs onChange listener?如何将多个参数传递给 Inputs onChange 监听器?
【发布时间】:2020-12-21 16:17:28
【问题描述】:

我使用的是 React 钩子,而不是基于类的方法。

所以我有一个 Person 组件,它分配了一个 Input 元素。我需要将事件(从中提取新/更改的值)和索引传递给侦听器函数。

如何同时传递事件和索引?

return (
            <Person
//other attributes
              change={renamePersonHandler.bind(this, (event, index))}
            />
          );

onChange 监听器。

   const renamePersonHandler = (event, id) => {
    // does stuff
  };

【问题讨论】:

    标签: reactjs input onchange


    【解决方案1】:

    想通了。输入元素值会自动传递给函数。只需绑定它并指定另一个参数就足够了。

         return (
                <Person
    //other attributes
                  change={renamePersonHandler.bind(this, person.id)}
                />
                );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多