2 回答

TA貢獻1860條經驗 獲得超9個贊
您誤解了 的類型children并混淆了使用defaultProps:
// Use for initial value
CartProvider.defaultProps = {
counter: 10,
/*
You declared the initial value to be the value of `PropTypes.string`
children: PropTypes.string
*/
}
// Children are always an Array of `ReactElement`/ `ReactElement` node.
CartProvider.propTypes = {
counter: PropTypes.number,
children: PropTypes.node.isRequired,
/* Same
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
]).isRequired
*/
/*
props.children can't be a string.
children: PropTypes.string,
*/
}

TA貢獻1859條經驗 獲得超6個贊
defaultProps 應該設置一個值而不是再次使用 Proptypes
CartProvider.propTypes = {
children: PropTypes.string,
}
CartProvider.defaultProps = {
children: "This is chidlren default string"
}
添加回答
舉報