简体   繁体   English

重用渲染道具中的React组件

[英]Reuse React component from render props

I wrote a react component in render props way,it will call children function with 3 react component object ( not sure the name exactly, the variable generated by executing jsx (<div>...</div>) ); 我以渲染道具的方式编写了一个react组件,它将使用3个react组件对象(不确定名称,确切地说是通过执行jsx (<div>...</div>)生成的变量)来调用子函数;

<PaginatedTable> Usage example: <PaginatedTable>用法示例:

<PaginationTable data={data} ...otherprops>
    {({ SearchBar, Table, PaginationBar })=>
        (<div>
            {SearchBar}
            {Table}
            {PaginationBar}
        </div>)
    }
</PaginationTable>

with render props, I'm so glad that I can custom these 3 child component object very easily such as rearrange order or adding custom elements between these three. 使用渲染道具,我很高兴可以非常轻松地自定义这三个子组件对象,例如重新排列顺序或在这三个子组件之间添加自定义元素。

{({ SearchBar, Table, PaginationBar })=>
        (<div>
            {PaginationBar}
            <h1> my custom search bar text </h1>
            {SearchBar}
            {Table}

        </div>)
    }

But now I wish more than arrange order inside , I wish I can move {SearchBar} out of to the same layer of 's sibling 's children such as this picture. 但是,现在我不仅希望在内部安排顺序,还希望可以将{SearchBar}移到该图片的同级孩子的同一层。

working demo: https://codesandbox.io/s/23q6vlywy 工作演示: https : //codesandbox.io/s/23q6vlywy

在此处输入图片说明

I thought this may be anti-pattern to the unidirectional data flow of React. 我认为这可能是React的单向数据流的反模式。

Extract {SearchBar} to another independent component then use it as <SearchBar ... /> inside of <ToolBarArea /> is what I learnd from React Doc. {SearchBar}提取到另一个独立组件,然后将其用作<ToolBarArea />内的<ToolBarArea /> <SearchBar ... /><ToolBarArea />是我从React Doc中学到的。

But in this way, I have to do "lifting state up" and write similar function and states already had in <PaginationTable /> like below **text** parts are functions and states already had in <PaginationTable /> 但是以这种方式,我必须“提升状态”并编写类似在<PaginationTable />已经存在的功能和状态,如下所示** text **部分是在<PaginationTable />已经具有的功能和状态

class ToolBarArea extends Component{
    render(){
        return(
        <div>
            // text
            <SearchBar onChange={**this.props.onSearchBarChange**} />
            //.... other text or elements
        </div>);
    }
}
class ContainerArea extends Component {
    state={
        **searchBarText:'',**
        tableData : [{...}, {...}]
    }
    **onSearchBarTextChange = (event)=>{
        this.setState({ searchBarText: event.target.value });
    }
    filterdTableData = ()=> this.state.tableData.filter(d=>d.name.includes(this.state.searchBarText);
    **
}

I really hope there is a way I can simply move the variable {SearchBar} in the render props function out to without knowing is in the parent or parent's sibling or anywhere in the DOM tree. 我真的希望有一种方法可以简单地将render props函数中的变量{SearchBar}移到其中,而无需知道是在父级或父级的同级中还是在DOM树中的任何位置。

such as

<ToolBarArea>
    {SearchBar} // SearchBar from <PaginationTable />
</ToolBarArea>

Is there a way to reuse onSearchBarTextChange and filtedTableData functions and these **text** codes I already wrote in <PaginationTable /> ? 有没有一种方法可以重用onSearchBarTextChangefiltedTableData函数以及我已经在<PaginationTable />编写的这些** text **代码?

I believe you hit the nail on the head when you referred to lifting state. 我相信提到提起状态时,您会碰到头。 If you already wrote a similar function then your best option may be to 'abstract' that function so that it applies to both use cases. 如果您已经编写了类似的函数,那么最好的选择是“抽象”该函数,以便将其应用于两个用例。 You could use a simple flag to differentiate the unique execution each needs. 您可以使用一个简单的标志来区分每种需要的唯一执行。 Then finally pass the function down to both components. 然后,最后将该函数传递给这两个组件。

If you're adamant about avoiding this approach you could technically get around it by using event listeners to handle data transfer or watch variables in the window but this is for sure an anti-pattern. 如果您坚决避免使用这种方法,那么从技术上讲,您可以使用事件侦听器来处理数据传输或监视window变量,从而解决该问题,但这肯定是一种反模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM