Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to put propTypes and defaultProps as static props inside React class?

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.

like image 433
ffxsam Avatar asked Apr 21 '16 18:04

ffxsam


People also ask

Is defaultProps deprecated?

Because defaultProps on functions will eventually get deprecated.

Which keyword lets you call propTypes from inside a class?

In class components, it's common to define propTypes inside the class with the static keyword.

Should I use defaultProps React?

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 .

Is propTypes necessary in React?

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.

What are proptypes in react?

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.

What is the use of defaultprops in react?

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.

How to set a default value for props in a class?

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.

Why do we use props in react?

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.


2 Answers

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 :)

like image 55
dandel Avatar answered Nov 06 '22 01:11

dandel


Oh yes, it's totaly legit with Babel (or other transpilers)

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.

like image 41
Qwerty Avatar answered Nov 06 '22 03:11

Qwerty