react组件设计和分解

react组件设计和分解

1.切割render

const PanelHeader = (props) => (
// ...
)

const PanelBody = (props) => (
// ...
)
class Panel extends React.Component {
    render() {
        return (
            <div>
                <PanelHeader title={this.props.title}/>
                <PanelBody content={this.props.content}/>
            </div>
        );
    }
}

2.模块化组件

相当于一个模版,根据传入参数进行不同的渲染
class commentTemplate extends React.Component(){
    static propTypes = {
        actions:propTypes.node
    }

    render(){
        <div>{this.props.actions}</div>
    }
}

class comment extends React.Component(){
    render(){
        const actions = [];

        if(this.props.condition){
        actions.push(<Delete/>)
        }else{
        actions.push(<Add/>)
        }
        return <commentTemplate actions={actions}/>
    }
}

3.高阶组件

高阶组件就是一个函数,且该函数接受一个组件作为参数,并返回一个新的组件
function noId() {
  return function(Comp) {
    return class NoID extends Component {
      render() {
        const {id, ...others} = this.props;
        return (
          <Comp {...others}/>
        )
      }
    }
  }
}

const WithoutID = noId()(Comp);

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容