- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 react 还很陌生,我有一个问题。我有一些代码用一些信息填充一些选项卡,我需要一些帮助来创建一个可以多次重用的函数,而不是为每个选项卡重用相同的代码。
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>{count}</dd>
<dt>Average Length</dt>
<dd>{avg_length}</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
{this.props.raw_strings}
</pre>
</div>
</div>
</div>
我想我可以创建一个 populateTabs 函数,它可以将计数、平均长度和 props 中的原始字符串数据作为参数。每个选项卡的 count、avg_length 和 raw_strings 都不同,因为它们各自代表不同的字符串类型,因此我一直在为每个选项卡重复使用此 block ,尽管只更改了 3 个变量。在这种情况下减少代码重用的最佳方法是什么?谢谢!
最佳答案
代码可以提取到组件中。如果某些参数在某些情况下是通用的,则可以是接受通用参数的高阶组件:
const boxHOC = (count, avg_length) => props => (
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>{count}</dd>
<dt>Average Length</dt>
<dd>{avg_length}</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
{props.raw_strings}
</pre>
</div>
</div>
</div>
);
const OneTwoBox = boxHOC(1, 2);
const ThreeFourBox = boxHOC(3, 4);
关于reactjs - 如何使这个 JSX 代码更加通用以避免 React 中的代码重用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55034388/
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
我正在寻找清理 Grails Controller 代码的方法。在各种 Controller 中我或多或少有相同的逻辑.. 获取对象 检查是否存在 等等.. 是否有建议的方法可以使 Controlle
我真的很喜欢 PHP,因为: _ 易于开发 Web 应用程序(您可以在 10 分钟内设置 LAMP,然后就可以开始了) _ 简单易学 _ 易于部署(您只需要带有 PHP 模块的 Apache) 我真的
我正在尝试使用 mod_rewrite 将我的博客 URL 转换为更适合 SEO 的格式。我所有的文章都存储在一个简单的 MySQL 数据库中。每个博客文章的网址如下所示: http://www.te
我是一名优秀的程序员,十分优秀!