Optional props arrow function

Viewed 135

I would like to make a generic component. For this I would like to be able to pass optional parameter to the props. I use to make it by using interface in typescript, but this project is in pure react. Is there a way to do something like typescript to make parameters optional ? The way I used to do in typescript is like that

interface buttonProps {
    title: string,
    color?: string
}
1 Answers

All props are optional unless you define PropTypes and mark them isRequired.

Typechecking with Proptypes

Example:

import PropTypes from 'prop-types';

const buttonPropTypes = {
  title: PropTypes.string.isRequired, // required
  color: PropTypes.string,            // optional
};

...

Button.propTypes = propTypes;

export default Button;
Related