當我閱讀有關來自開源的道具時,我看到使用props
和propTypes
來聲明從父級獲得的屬性。兩者有什麼區別?哪一個應該使用?ReactJS - 道具或propTypes?
例如:
export default class App extends Component {
props: {
children: Children
};
render() {
return (
<div>
{this.props.children}
</div>
);
}
}
propTypessource code
export default class Board extends Component {
static propTypes = {
getLists: PropTypes.func.isRequired,
moveCard: PropTypes.func.isRequired,
moveList: PropTypes.func.isRequired,
lists: PropTypes.array.isRequired,
}
...
}
在您鏈接的道具文檔中,它說道具是只讀的。所以,我想我不應該在我的問題的第一個例子中使用它。我可以使用'defaultProps'來代替。感謝您發佈SO文檔!不知道這樣的事情存在。 – tsuz