Leo Messi
Leo Messi

Reputation: 6186

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. The problem is that the function is not defined yet so it must receive some 'empty' function in order to pass the tests.

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.

Is there a way to put a function that doesn't do anything just to pass the linting?

Upvotes: 1

Views: 1220

Answers (3)

Dru
Dru

Reputation: 11

Maybe try returning void instead?

onClick={() => void 0}

Upvotes: 0

Arif Khan
Arif Khan

Reputation: 508

Use this:

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

Upvotes: 1

hellogoodnight
hellogoodnight

Reputation: 2139

The function can just return null:

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

Upvotes: 2

Related Questions