Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set defaultProps for function?

Tags:

reactjs

Is it possible to set defaultProps for functions? Code below doesn't work. It doesn't like 'this'. I tried 1_ bind defaultProps to 'this', or 2_ bind handleClose to 'this', neither works. I use ES6 class.

SaveDialog.propTypes = {
  note: React.PropTypes.string,
  onCancel: React.PropTypes.func,
  onSave: React.PropTypes.func,
  onEditChange: React.PropTypes.func
};

SaveDialog.defaultProps = {
  note: '',
  onCancel: {this.handleClose},
  onSave: {this.handleClose},
  onEditChange: {this.handleChange}
};
like image 943
jay.m Avatar asked Feb 16 '16 15:02

jay.m


People also ask

How do you set a default prop in a function?

We can define default values to props using the defaultProps property. defaultProps is used to ensure that props will have a value if it was not specified by the parent component. The propTypes type-checking happens after defaultProps are resolved, so type-checking will also apply to the defaultProps.

Can we use PropTypes in functional component?

In this example, we are using a class component, but the same functionality could also be applied to function components, or components created by React.memo or React.forwardRef . PropTypes exports a range of validators that can be used to make sure the data you receive is valid.

How will you set state in render method?

render() Calling setState() here makes it possible for a component to produce infinite loops. The render() function should be pure, meaning that it does not modify a component's state. It returns the same result each time it's invoked, and it does not directly interact with the browser.

Can we use default props in functional component?

Default props can be used to define any props that you want to be set for a component, whether or not a value is actually passed in from the parent component.


1 Answers

propTypes is a validator. We use propTypes to document the intended property types passed to components. React checks the props passed to components against those definitions, and will result in a warning/error when they don’t match.

We can define default values to props using the defaultProps property. defaultProps is used to ensure that props will have a value if it was not specified by the parent component. The propTypes type-checking happens after defaultProps are resolved, so type-checking will also apply to the defaultProps.

Here we have

SaveDialog.propTypes {
  note: PropTypes.string,
  onCancel: PropTypes.func,
  onSave: PropTypes.func,
  onEditChange: PropTypes.func,
};

React is making sure that the note prop is a string, and is making sure the onSave, onCancel, and onEditChange props are functions. If not, it will result in a warning/error.

Here we have

SaveDialog.defaultProps {
  note: '',
  onCancel: {this.handleClose},
  onSave: {this.handleClose},
  onEditChange: {this.handleChange},
};

In SaveDialog.propTypes, note has been declared as a string. Let’s assume that a prop has not been specified by the parent container. Since note has no value, we will use the default value provided in defaultProps. The default value of note is an empty string. When type-checking occurs, we see that the prop is intended to be a string and is, in fact, an empty string. No problems here, let’s move on.

Next up, onCancel in SaveDialog.PropTypes {...}; onCancel has been declared as a func (function). Assuming that a prop has not been specified by the parent container, we’ll use the default value of onCancel in defaultProps. The default value is this.handleClose.

Let’s talk about what’s going on here. To put it simply, this is referring to the object that invoked the function.

In that case, defaultProps does not have access to the onCancel function, and because onCancel wasn’t defined within defaultProps, it returns undefined, or an error/warning.

If that's the case, all we have to do is define what we want onCancel to be within defaultProps.

So we can declare the functions within defaultProps or we can have the default value be an empty function making it clear that onCancel, onSave or the onEditChange functions are empty functions that return undefined.

SaveDialog.defaultProps {
  note: '',
  onCancel: () => {},
  onSave: () => {},
  onEditChange: () => {},
};

or

SaveDialog.defaultProps {
  note: '',
  onCancel: () => {
        // handleClose function here
  },
  onSave: () => {
    // handleClose function here
  },
  onEditChange: () => {
    // handleChange function here
  },
};
like image 172
Darryl J. Zeigler Avatar answered Oct 10 '22 12:10

Darryl J. Zeigler