React 功能组件默认道具与默认参数 [英] React functional component default props vs default parameters
本文介绍了React 功能组件默认道具与默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 React 功能组件中,这是设置默认 props 的更好方法,使用 Component.defaultProps
,或使用 默认参数关于函数定义,例子:
In a React functional component, which is the better approach to set default props, using Component.defaultProps
, or using the default parameters on the function definition, examples:
默认道具:
const Component = ({ prop1, prop2 }) => (
<div></div>
)
Component.defaultProps = {
prop1: false,
prop2: 'My Prop',
}
默认参数:
const Component = ({ prop1 = false, prop2 = 'My Prop' }) => (
<div></div>
)
推荐答案
defaultProps
在功能组件上将 最终会被弃用(根据核心团队之一丹·阿布拉莫夫的说法),因此为了面向未来,值得使用默认参数.
defaultProps
on functional components will eventually be deprecated (as per Dan Abramov, one of the core team), so for future-proofing it's worth using default parameters.
这篇关于React 功能组件默认道具与默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文