简体   繁体   English

如何从 React 中的组件事件中删除匿名 function?

[英]How to remove an anonymous function from a component event in React?

In my component, I have:在我的组件中,我有:

<StyledPill
    key={`styled-pill-${idx}`}
    size="small"
    label={searchType.label}
    onClick={() => handleClick(idx + 1)}
    featured={selectedSearchType === idx + 1}
/>

where:在哪里:

const handleClick = (index: number) => {
    setSelectedSearchType(index);
};

However, I was told that we don't want to have anonymous functions () => handleClick(idx + 1) .但是,有人告诉我,我们不想使用匿名函数() => handleClick(idx + 1) Is there some performant way to avoid it?有没有一些有效的方法来避免它?

I can't do onClick={handleClick} because then I can't pass the argument.我不能做onClick={handleClick}因为那样我就不能传递参数。

Looking for some help - any would be appreciated.寻求一些帮助 - 任何将不胜感激。 Thanks!谢谢!

You can avoid creating an anonymous handler for each element by making the per-item data available on the element, and reading it in the event handler.您可以避免为每个元素创建匿名处理程序,方法是使每个项目的数据在元素上可用,并在事件处理程序中读取它。

However, this is a premature optimization that you probably don't need.但是,这是您可能不需要的过早优化。

function Component({ searchTypes, selectedSearchType, handleClick }) {
  const handleItemClick = React.useCallback((event) => {
    const itemId = parseInt(event.target.dataset.index);
    handleClick(itemId);
  }, [handleClick]);
  return (
    <>
      {searchTypes.map((searchType, idx) => (
        <StyledPill
          key={`styled-pill-${idx}`}
          size="small"
          label={searchType.label}
          data-index={idx + 1}
          onClick={handleItemClick}
          featured={selectedSearchType === idx + 1}
        />
      ))}
    </>
  );
}

Hmm, if you really would like to do it and have access to the StyledPill component, an option could be to accept onClick and index as props, then write a wrapper function to call the onClick with the index .嗯,如果你真的想这样做并且可以访问StyledPill组件,一个选项可能是接受onClickindex作为道具,然后编写一个包装器 function 以调用onClick index ZB73CAB20CE55A0034836D0。

<StyledPill
    key={`styled-pill-${idx}`}
    size="small"
    label={searchType.label}
    onClick={handleClick}
    index={idx + 1}
    featured={selectedSearchType === idx + 1}
/>
// StyledPill.jsx

function StyledPill({onClick, index}) {
    function handleOnClick() {
        onClick(index);
    }
    
    return (
        <button onClick={handleOnClick} .../>
    )
}

If you just want to pass a named function to the onClick handler you could define the function inside your map() callback:如果您只想将命名的 function 传递给onClick处理程序,您可以在map()回调中定义 function :

function Component({ searchTypes, selectedSearchType, handleClick }) {
  const handleClick = (index: number) => {
    setSelectedSearchType(index);
  };

  return (
    <>
      {searchTypes.map((searchType, idx) => {
        const handlePillClick = () => handleClick(idx + 1);

        return (
          <StyledPill
            key={`styled-pill-${idx}`}
            size="small"
            label={searchType.label}
            data-index={idx + 1}
            onClick={handlePillClick}
            featured={selectedSearchType === idx + 1}
          />
        );
      })}
    </>
  );
}

You could achieve that like this, which is also called point-free style您可以像这样实现,这也称为无点样式

const handleClick = (index: number) => () => {
    setSelectedSearchType(index);
};

// ...

<StyledPill
    key={`styled-pill-${idx}`}
    size="small"
    label={searchType.label}
    onClick={handleClick(idx + 1)}
    featured={selectedSearchType === idx + 1}
/>

Codesandbox Demo Codesandbox 演示

编辑希望-northcutt-2ucy4q

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM