Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Question: propTypes? #77

Closed
tvararu opened this issue Nov 28, 2015 · 7 comments
Closed

Question: propTypes? #77

tvararu opened this issue Nov 28, 2015 · 7 comments

Comments

@tvararu
Copy link

tvararu commented Nov 28, 2015

I love this.

My only question about using function components, how do you specify propTypes? For documentation purposes chiefly, but they also help catch stupid mistakes.

This works but it doesn't look that great:

const Text = ({ children }) => 
  <p>{children}</p>
Text.propTypes = { children: React.PropTypes.string };
Text.defaultProps = { children: 'Hello World!' };

I saw that you use Flow type notation in the documentation. Is that a possible route? Replacing propTypes with Flow annotations entirely?

@mrblueblue
Copy link
Contributor

At least for me, I like to have propTypes at the top of the file, so I've been writing my components this way.

const _propTypes = { children: React.PropTypes.string };
const _defaultProps = { children: 'Hello World!' };

const Text = ({children}) => (
  <p>{children}</p>
);

export default compose(
  setPropTypes: _propTypes,
  setDefaultProps: _defaultProps
)(Text)

Where setDefaultProps is just function like so:

const setDefaultProps = setStatic('defaultProps')

It makes more sense when you have a lot of propTypes you want to specify.

I'd be curious to see what other patterns people are using!

@tvararu
Copy link
Author

tvararu commented Nov 28, 2015

setPropTypes() - Assigns to the propTypes property on the base component.

I feel embarassed, I didn't see this function in the API documentation. Should have cmd+F'ed for propTypes before posting this.

@mrblueblue: both your method and the one using the setPropTypes function do look fine.

Keeping issue open for the sake of discussion, feel free to close it Andrew if you consider it resolved.

@acdlite
Copy link
Owner

acdlite commented Nov 30, 2015

I believe the long term plan for React is to deprecate propTypes and use Flow instead.

@acdlite acdlite closed this as completed Nov 30, 2015
@ericwooley
Copy link

I know this is closed, but @acdlite do you have flow definitions somewhere?

@hannupekka
Copy link

hannupekka commented Oct 4, 2016

I'd be interested also to know how to use for example pure with Flowtype. At the moment Flowtype does not warn me when component is missing a required prop.

Edit: found this PR #241 - nevermind :)

@idris
Copy link
Contributor

idris commented Jun 2, 2017

@acdlite is recompose now compatible with Flow's React integration? https://flow.org/en/docs/frameworks/react/

In other words, can we omit propTypes if we use Flow?

@wuct
Copy link
Contributor

wuct commented Jun 4, 2017

@idris I don't think so. We need flow type definition for recompose which is work-in-progress under #241.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants