Checking the types of support in the reactor

I read the React.Component section in the official React documentation. Everything made sense, except for the part regarding propTypes . The docs indicate the following:

In production mode, propTypes checks are skipped to increase efficiency.

Let's say I have the following code:

 class Sample extends React.Component { render() { return ( <div>Hello {this.props.name}</div> ); } } Sample.propTypes = { name: React.PropTypes.string }; 

Do the documents say that during the production process my props will be skipped? If so, how should I check for support types?

+5
source share
2 answers

You do not check the types of support themselves; React does this for you.

However, as the docs say, as long as you are in development mode. Each prop type check is a function call that uses processing power and memory.


While you are in development, knowing that one of your details is of the wrong type, this is worth a worthy compromise.

Once you are in production, your application needs to be tested thoroughly enough so that none of your prop statements work anyway.

For this reason, they are skipped to make your application more efficient.

+5
source

propTypes are only used to check your current development process. You receive a notification through the console logs when there are no required properties, or the type of the property is not as expected.

There is no need to verify this type of verification in production assemblies. Missing properties do not interfere with the rendering process; they simply render your component in an unexpected way. But note that the rendering process will fail when you need to access the properties of the “given” object.

To take control of this unexpected behavior, you must declare default properties.

However, it is recommended that you work with the propTypes object and use unit tests to avoid unexpected rendering behavior. Adhere to

+2
source

Source: https://habr.com/ru/post/1260946/


All Articles