ReactJS defaultProps empty function declaration

Viewed 3668

What is the best practice defining a function defaultProps that is empty in ReactJS?

My solution so far is either an empty arrow function or a null value. Which way would be better?

MyComponent.defaultProps = {
  onClick: () => {},
  onClickNull: null,
};
2 Answers

I don't think there is a right answer for this.

If you want to be more explicit in your code, go with the null and check if the function is null before calling it.

If you want to have less code, go with empty function.

The more important thing I would say, is to be consistent in the entire project.

Important thing to keep in mind is that an empty function ()=>{} is an object hence its truthy whereas null and undefined are falsy.

So if you want to check whether that event handler prop is passed on implementation and handle both cases (supplied and not supplied) differently, it makes more sense to go with null or undefined.

Related