javascript - 如何通过捷径控制道具?
问题描述
我想知道我们怎样才能使这个案例变得容易?
我有一个自定义的 Typeahead 组件,这将对我的自定义进行一些操作。所以我想通过 TypeaheadCustom 组件的 props 来控制 Typeahead 的 props:
在组件中,我调用 TypeaheadCustom :
<TypeaheadCustom propsControler />
在 TypeaheadCustom 组件中:
function TypeaheadCustom({ propsControler ,children, ...otherProps }, ref) {
if(propsControler=== true) { //make some propsOfTypeahead}
return (
<Fragment>
<Typeahead ref={ref} {...otherProps} //propsOfTypeahead>
{children}
</Typeahead>
</Fragment>
);
}
}
我想要完全这样做,因为我想要它干净。我们怎样才能做到这一点?谢谢
解决方案
您可以为此编写一个 HoC。在 HoC 中,您通过 propsControler。如果为真,则返回组件,否则返回 null。这是伪代码
`
function TypeaheadCustom(propsControler, childrenComponent, ref) {
return propsControler === true ? (<Fragment>
<Typeahead ref={ref} {...otherProps}> //propsOfTypeahead>
{<childrenComponent />} // It shall have the props of its own
</Typeahead>
</Fragment>) : null
}
}
`
推荐阅读
- reactjs - 如何放置 Graphql 查询以从反应组件中的 Wordpress API 获取数据,而不是 GatsbyJS 中的模板?
- python - Python 2.7 PIP 安装以使用 Artifactory 作为代理
- mysql - 如何在完全导入期间在 Apache Solr 中创建新列?
- python - 我想从 HTML 中抓取数据并将其转换为 pandas DataDrame 最后将其存储为 aCSV 文件
- unity3d - 为什么刚体上的 OnMouseDown() 事件不会触发?
- laravel - 每个租户的多租户和自定义功能
- sql - 仅显示 MAX 行
- algorithm - 图中所有可能的路径
- arrays - 如何在执行快速排序时正确找到枢轴元素?
- javascript - 数字计数器将数字从 1 增加到最大值,然后从该值减少到 1。但它应该增加然后停止