这是定义属性的唯一方法吗?
<YAxis
id="axis1"
label="Requests"
style={{ labelColor: scheme.requests }}
labelOffset={-10}
min={0}
max={1000}
format=",.0f"
width="60"
type="linear"
/>
如果有一长串的属性,这可能会很混乱。它使我想起了一些内联CSS,这可能变得很难阅读。
我们不能只是说:
var yAxis_props = ( id = ... label = ... )
然后像这样插入它们:
<YAxis yAxis_props />
是的,可以将其定义yAxis_props
为具有所有键值的对象。像这样:
var yAxis_props = {
id: '',
label: ''
}
然后使用传播运算符将所有值作为单独的prop传递,如下所示:
<YAxis
{...yAxis_props}
/>
检查React Doc。
好的,您对这两种方式感到困惑:
// first way
<Component id='', label='' />
// second way
const obj = { id:'', label: ''}
<Component {...obj} />
背后的场景都是一样的。检查这两种方式的Babel repl输出。我们写jsx
,这将被转换成React.createElemet(component/html tag, props, children)
。
码:
function ComponentA () {
return( <div>Hello</div> )
}
function ComponentB() {
return (
<div>
<A id='a' label='b' />
<A {...{id:'a', label:'b'}} />
</div>
)
}
转换形式:
function ComponentA() {
return react("div", null, "Hello");
}
function ComponentB() {
return react(
"div",
null,
react(A, {
id: "a",
label: "b"
}),
react(A, {
id: "a",
label: "b"
})
);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句