samuel potter
samuel potter

Reputation: 209

Optional props arrow function

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
}

Upvotes: 1

Views: 346

Answers (1)

Drew Reese
Drew Reese

Reputation: 203091

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;

Upvotes: 1

Related Questions