Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

[react]有a,b俩组件和一个按钮c,功能上c属于a,但UI显示上需要显示在b里面,如何处理?

或者,换个说法,原本有a,b俩组件(平级关系),a组件里面有个按钮c,某天设计师要求把这个按钮c移动到组件b里面去,功能完全不变,怎么优雅处理


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

没明白你的意思,不过按照字面意思就是这样。

<Bcomponent>
    <Acomponent justRenderC={true}>
        { justRenderC ? <Cbutton></Cbutton> : <SomeThingOther /> }
    </Acomponent>
</Bcomponent>

那就是要求Cbutoon的功能独立,可以使用HOC的形式。

const HOC = props => WarpComponent => {
    return (
        <WarpComponent>
            <Cbutton/>
        </WarpComponent>
    )
}

const Acomponent = HOC()(A)
const Bcomponent = HOC()(B)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...