Created
March 10, 2017 05:00
-
-
Save jamiebuilds/1caedc42119cc4e493493b1632dc619e to your computer and use it in GitHub Desktop.
React PropTypes additions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {PropTypes} from 'react'; | |
class MyComponent extends React.Component { | |
static propTypes = { | |
foo: PropTypes.string, | |
bar: PropTypes.number.deprecated("Use props.foo instead of props.bar"), | |
}); | |
} | |
<MyComponent foo="test"/>; // Works! | |
<MyComponent bar={3.14}/>; // Deprecation Warning: Use props.foo instead of props.bar (MyComponent) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {PropTypes} from 'react'; | |
class MyComponent extends React.Component { | |
static propTypes = PropTypes.exact({ | |
foo: PropTypes.string, | |
bar: PropTypes.number, | |
}); | |
} | |
<MyComponent foo="test" bar={42}/>; // Works! | |
<MyComponent foo="test" bar={42} bat={true}/>; // Error! Prop "bat" found in exact props. | |
// Or using Flow... (https://flowtype.org/) | |
class MyComponent extends React.Component { | |
props: {| | |
foo?: string, | |
bar?: number | |
|}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {PropTypes} from 'react'; | |
class MyComponent extends React.Component { | |
static propTypes = { | |
foo: PropTypes.string, | |
bar: PropTypes.shape({ | |
baz: PropTypes.number | |
}).exact | |
}; | |
} | |
<MyComponent foo="test" bar={{ baz: 42 }}/>; // Works! | |
<MyComponent foo="test" bar={{ baz: 42, bat: 3.14 }}/>; // Error! Property "bat" found in exact object. | |
// Or using Flow... (https://flowtype.org/) | |
class MyComponent extends React.Component { | |
props: { | |
foo: string, | |
bar: {| | |
baz: number | |
|} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🙏