假设我们使用一个组件Abc
作为:
<Abc>
{
param => {xxx} // anonymous arrow function
}
</Abc>
那么Abc
组件如何在内部使用匿名函数?我的意思是在Abc
组件的源代码中,
export class Abc extends Component {
...
this.??.?? // how to invoke the anonymous function whenthe function doesn't even have a name?
}
这是一种称为渲染道具的技术。
function Abc({ children }) {
return children('data from abc')
}
可以用作
<div>
<Abc>{dataFromAbc => dataFromAbc}</Abc>
</div>
请记住
<Abc>{dataFromAbc => dataFromAbc}</Abc>
相当于
<Abc children={dataFromAbc => dataFromAbc} />
这样就可以从children
道具中调用您的函数。this.props.children()
如果您在班级组件中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句