当两个组件共享某些相同的方法但布局不同时,如何避免编写相同的代码?如何避免在具有不同布局的组件中编写相同的逻辑?
下面的示例组件具有一个方法“renderLastItem”,它使用由父组件传递的prop“something”。
我想过使用高阶组件模式,但我不确定我可以传递道具作为高阶组件的参数。
下面的示例代码非常简单,所以在此示例代码中,我只需要使用If语句并根据组件类型更改布局,但在实际代码中,我有更多代码,我想避免使用if语句来根据组件类型更改布局。
如何避免在多个组件中编写相同的逻辑?
ComponentA
import React, { Component } from 'react';
import PropTypes from 'prop-types';
const propTypes = {};
const defaultProps = {};
class SampleA extends Component {
constructor(props) {
super(props);
}
renderLastItem() {
if(!this.props.something) {
return null;
}
return this.props.something[this.props.something.length - 1];
}
render() {
return (
<div>
<h1>Something</h1>
<p>{this.renderLastItem()}</p>
</div>
);
}
}
SampleA.propTypes = propTypes;
SampleA.defaultProps = defaultProps;
export default SampleA;
以componentB
import React, { Component } from 'react';
import PropTypes from 'prop-types';
const propTypes = {};
const defaultProps = {};
class SampleB extends Component {
constructor(props) {
super(props);
}
renderLastItem() {
if(!this.props.something) {
return null;
}
return this.props.something[this.props.something.length - 1];
}
render() {
return (
<div>
<ul>
<li>Something</li>
<li>Something else</li>
<li>{this.renderLastItem()}</li>
</ul>
</div>
);
}
}
SampleB.propTypes = propTypes;
SampleB.defaultProps = defaultProps;
export default SampleB;
非常感谢你!所以在我的例子中,我会写代码“export default( )”? –
hytm
这种模式很酷的事情之一是,您可以将它作为组件固有的概念应用 - 在这种情况下,您可以做类似的事情 - 或者作为组件父项的固有概念,其中你只需要在渲染时应用它。无论哪个对你最有意义! – Hamms