我正在阅读 Web 应用程序的代码库,并且在代码中看到一些静态 PropType。 我无法理解它解决了什么问题或者为什么需要它们?
这是相同的代码。
static propTypes = {
fetchCricketFantasyPlayers: PropTypes.func.isRequired,//I see this in action
selectedAlpha: PropTypes.array,// this comes from reducer or say redux
history: PropTypes.object.isRequired // this seems to be related to redirecting etc.
};
最佳答案
<强> Static 不适合 React,它是 JavaScript 的一部分,根据 MDN 的说法:
The static keyword defines a static method for a class. Static methods aren't called on instances of the class. Instead, they're called on the class itself. These are often utility functions, such as functions to create or clone objects.
这里有两种声明 propTypes 的方法,并且两者的工作方式相同:
class App extends React.Component {
render() {
return null
}
}
App.propTypes = {
fetchCricketFantasyPlayers: PropTypes.func.isRequired,//I see this in action
selectedAlpha: PropTypes.array,// this comes from reducer or say redux
history: PropTypes.object.isRequired
}
使用静态:
class App extends React.Component {
static propTypes {
fetchCricketFantasyPlayers: PropTypes.func.isRequired,//I see this in action
selectedAlpha: PropTypes.array,// this comes from reducer or say redux
history: PropTypes.object.isRequired
}
render() {
return null
}
}
关于javascript - 在reactjs中使用静态propTypes有什么意义?它能解决任何问题吗?或者它只是一种模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55844074/