简体   繁体   English

如何创建在 React 中不执行任何操作的 function

[英]How to create a function that doesn't do anything in React

There is a component that must be build which must receive a function for onClick.有一个必须构建的组件必须接收 onClick 的 function。 The problem is that the function is not defined yet so it must receive some 'empty' function in order to pass the tests.问题是 function 尚未定义,因此它必须接收一些“空” function 才能通过测试。

This is the code:这是代码:

  <Button
    title='MyTitle'
    className='my-class-name'
    onClick={
      () =>
        /* eslint-disable no-console */
        console.log('test')
      /* eslint-enable no-console */
    }
  />

So I added a console.log() but eslint doesn't like it so the comments before and after it were added.所以我添加了一个console.log()但 eslint 不喜欢它,所以添加了它之前和之后的评论。

Is there a way to put a function that doesn't do anything just to pass the linting?有没有办法放置一个不做任何事情的 function 只是为了通过棉绒?

The function can just return null: function 可以只返回 null:

   <Button
        title='MyTitle'
        className='my-class-name'
        onClick={() => null}
    />

Maybe try returning void instead?也许尝试返回 void 代替?

onClick={() => void0}

Use this:用这个:

<Button
    title='MyTitle'
    className='my-class-name'
    onClick={
      () => undefined
    }
  />

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

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