This is the way I've been doing it for quite some time now:
export default class AttachmentCreator extends Component { render() { return <div> <RaisedButton primary label="Add Attachment" /> </div> } } AttachmentCreator.propTypes = { id: PropTypes.string, };
But I've seen people doing it this way:
export default class AttachmentCreator extends Component { static propTypes = { id: PropTypes.string, }; render() { return <div> <RaisedButton primary label="Add Attachment" /> </div> } }
And in fact I've seen people setting initial state outside the constructor as well. Is this good practice? It's been bugging me, but I remember a discussion somewhere where someone said that setting default props as a static is not a good idea - I just don't remember why.
Because defaultProps on functions will eventually get deprecated.
In class components, it's common to define propTypes inside the class with the static keyword.
If you want the default values to be applied for every method, such as the ones from the React life-cycle ( shouldComponentUpdate , etc.) then you must use the defaultProps instead. So, the previous code is actually a mistake that may lead to misunderstanding about the default value of name .
One of the most important things when building React application is to make sure that the components receive correct props. Passing wrong props leads to bugs and unexpected behavior, so it's a good idea to warn developers about this as early as possible.
PropTypes are a mechanism to ensure that components use the correct data type and pass the right data, and that components use the right type of props, and that receiving components receive the right type of props. We can think of it like a puppy being delivered to a pet store.
In cases where PropTypes are optional (that is, they are not using isRequired ), we can set defaultProps. Default props ensure that props have a value, in case nothing gets passed. Here is an example: Here, defaultProps will be used to ensure that this.props.name has a value, in case it is not specified by the parent component.
In class components, it's common to define propTypes inside the class with the static keyword. You can also set a default value for your props, with a defaultProps object. You need to sign up for Treehouse in order to download course files.
Because React comprises several components, props make it possible to share the same data across the components that need them. It makes use of one-directional data flow (parent-to-child components). However, with a callback function, it’s possible to pass props back from a child to a parent component.
In fact, it's exactly the same in terms of performance. React.JS is a relatively new technology, so it's not clear yet what are considered good practices or don't. If you want to trust someone, check this AirBNB's styleguide:
https://github.com/airbnb/javascript/tree/master/react#ordering
import React, { PropTypes } from 'react'; const propTypes = { id: PropTypes.number.isRequired, url: PropTypes.string.isRequired, text: PropTypes.string, }; const defaultProps = { text: 'Hello World', }; class Link extends React.Component { static methodsAreOk() { return true; } render() { return <a href={this.props.url} data-id={this.props.id}>{this.props.text}</a> } } Link.propTypes = propTypes; Link.defaultProps = defaultProps; export default Link;
They are declaring a const with the propTypes object literals, keep the class pretty clean and assign them later to the static properties. I personally like this approach :)
class DataLoader extends React.Component { static propTypes = { onFinishedLoading: PropTypes.func } static defaultProps = { onFinishedLoading: () => {} } }
...as it gets transpiled to this anyway.
class DataLoader extends React.Component {} DataLoader.propTypes = { onFinishedLoading: PropTypes.func }; DataLoader.defaultProps = { onFinishedLoading: () => {} };
Static fields get transpiled as properties on the class object under the hood. Look here at Babel REPL.
Moreover, assigning state or other class fields directly in the class body gets transpiled into the constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With