Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does React.js not have stateTypes?

Tags:

reactjs

React components conceptually split their data into props, immutable data passed from their parent, and state, mutable data maintained locally. One thing I like about React is its support for limited type checking in the form of propTypes. Why is there not a similar concept for state (e.g. stateTypes)?

like image 750
seanmcl Avatar asked Jul 04 '15 11:07

seanmcl


People also ask

Why we use StrictMode in React?

StrictMode is a React Developer Tool, primarily used to identify potential issues in a web application. For its descendant components, it activates additional deprecation checks and warnings.

Does react JS create a virtual DOM in the memory?

19) Does React. js create a VIRTUAL DOM in the memory? Answer: A is the correct option as React. js creates a VIRTUAL DOM in the memory.

How do I turn on strict mode in React?

React's StrictMode is sort of a helper component that will help you write better React components, you can wrap a set of components with <StrictMode /> and it'll basically: Verify that the components inside are following some of the recommended practices and warn you if not in the console.

Is React strict mode necessary?

The React StrictMode can be viewed as a helper component that allows developers to code efficiently and brings to their attention any suspicious code which might have been accidentally added to the application. The StrictMode can be applied to any section of the application, not necessarily to the entire application.


1 Answers

Using so-called stateTypes wouldn't give you much of a benefit.

As the official React site says about propTypes:

As your app grows it's helpful to ensure that your components are used correctly.

The important thing to remember here is that propTypes checks if you passed correct data to the current element in the place where you render this component. You can reuse the component as many times as you wish, therefore it could easily happen that you forgot to pass appropriate properties to it.

Therefore, checking data passed from "other source" is more important and more beneficial than checking the data you just use when you write the component itself. If you could work with the information from state somewhere else, which you cannot, it would be worth using.

Anyway, it's all just a practical tool for easier development, which must be turned off on the production environment.

Conclusion: maybe this question is a bit opinion-based. I believe, that using stateTypes wouldn't help much if you declared your state in constructor (ES2015) or getInitialState, and just several lines below the exactly same information, only in the form of stateTypes.

like image 188
marizikmund Avatar answered Sep 20 '22 21:09

marizikmund