Why does React.js not have stateTypes?

后端 未结 1 1791
甜味超标
甜味超标 2021-02-20 06:09

React components conceptually split their data into props, immutable data passed from their parent, and state, mutable data maintained locally. One thing I li

相关标签:
1条回答
  • 2021-02-20 06:18

    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.

    0 讨论(0)
提交回复
热议问题