reactjs - 如何使用扩展运算符使用作为道具传递的函数
问题描述
如何使用 React 中使用扩展运算符作为道具传递的另一个组件中的单个函数。
const Component=(props)=>
{
const A=()=>
{
}
const B=()=>
{
}
render(){
return(
<Anothercomponent RenderChild={(props)=>{<Component {...props} />}}
);
}
}
const Anothercomponent=()=>
{
render(){
const {RenderChild} = this.props;
return(
//how to consume function A here
);
}
解决方案
首先,你还没有通过 functionA
和B
as props
其次,您正在使用功能组件,因此您可以从函数参数而不是this.props
. 它还需要使用渲染方法。如果使用渲染,则需要将组件定义为类组件
const Anothercomponent=(props)=> {
const { RenderChild } = props;
return(
<div>Another component</div>
);
}
const Component=(props)=> {
const A=()=> {}
const B=()=> {}
return(
<Anothercomponent RenderChild={(props)=>{<Component {...props} />}}
);
}
推荐阅读
- javascript - 如何在返回中显示从其他函数返回的 JSX?
- arrays - 查找动态分配数组的大小
- postgresql - 升级后缺少 Postgres 扩展
- r - 有没有一种更简单的方法可以在 data.frame 中创建具有 2 个以上级别的因子变量?
- flutter - 无法取消选中复选框
- python - 将网络抓取数据转换为数据框
- couchbase - Couchbase 服务器:连接上的缓慢 GET 操作
- django - 带有 model_to_dict(self) 的 ManyToManyField
- json - 在嵌套的复杂 JSON 中搜索键
- rust - 为什么它使用不可变引用而不是 ssh2::Channel 读取文件?