【问题标题】:Pass dynamic value to higher order component将动态值传递给高阶组件
【发布时间】:2019-07-24 06:57:42
【问题描述】:

在我的代码中,我必须在不同的地方使用相同的代码行。所以我认为现在是将这段代码放入某种基类的正确时机。我已经阅读了有关Higher-Order Components 的信息,这似乎是可行的方法,并且按照一些示例,我最终得到了以下代码,但该代码不起作用。我已经尝试了一些方法,但无法让它工作。

我的 HOC:

export interface HocProps {
    DynamicId: string
}

const withDiv = (hocProps) => (BaseComponent) => {
    return class extend React.Component {
        render() {
            return (
                <div id={ hocProps.DynamicId }>
                    <BaseComponent />
                </div>
            );
        }
    }
}

export default withDiv;

要被 div 包裹的组件:

import withDiv from './MyHoc';

class MyComponent extends React.Component {
    render() {
        return (
            <h3>Some content here</h3>
        );
    }
}

export default withDiv({ DynamicId: <dynamic value> })(MyComponent);

另一个使用 MyComponent 的组件:

import MyComponent from './MyComponent';

export class OtherComponent extends React.Component {
    render() {
        return (
            <div>
                ... Some content here ...
                <MyComponent DynamicId={ 'id123' } />
            </div>
        );
    }
}

我想在 OtherComponent 中传递一个 id。然后在 MyComponent 中,这个 id 必须作为 传递给 HOC,这是行不通的。我只能将静态值传递给 HOC。

我是新手,我想我犯了同样的错误。

所以我的问题是:我做错了什么以及如何做对了? 也许还有另一种/更好的方法?

提前致谢。

编辑: 我希望得到这样的结果:

<div>
    ... Some content here ...
    <div id='id123'>
        <h3>Some content here</h3>
    </div>
</div>

【问题讨论】:

  • 您将OtherComponent 中的动态ID 传递到哪里?
  • 对不起。我已经更新了代码,但我不确定这是否可行。

标签: reactjs higher-order-components


【解决方案1】:

根据您的实现,您可以通过两种方式使用 HOC。 我创建了一个sandbox,它将帮助您了解如何使用 HOC。

一种方法是提取你的道具const hocWrapper = Component =&gt; props =&gt; { // return NewComponent and call it too }。在这里你必须在返回时调用你的组件。

另一种方法是解构或使用 hocWrappers 中的道具。 const hocWrapper = Component =&gt; { // return NewComponent, you will receive props inside the newComponent and do what you wish}

【讨论】:

  • 谢谢。以正确的方式处理道具就可以了。这让我很困惑。
【解决方案2】:

试试这个

const withDiv = (BaseComponent) => {
    class CompWithDiv extends React.Component {
        render() {
            return (
                <div id={this.props.DynamicId}>
                    <BaseComponent />
                </div>
            );
        }
    }
    return CompWithDiv ;
}

export default withDiv;

【讨论】:

    【解决方案3】:

    我不确定您如何传递动态值以及它有什么问题。但是,你可以像

    这样创建你的组件
    export interface MyCustomProps {
        customProp: string;
    }
    
    export interface MyCustomState {
        something: string;
    }
    
    class MyComponent extends React.Component<MyCustomProps, MyCustomState>{
      render(){<div>
        ... Some content here ...
        <div>{this.props.customProp}</div>
      </div>}
    }
    export default MyComponent
    

    并在另一个组件中使用它,例如

    import MyComponent from './MyComponent';
    
    export class OtherComponent extends React.Component {
      render() {
        return (
            <div>
                ... Some content here ...
                <MyComponent customProp={someDynamicStringValues}/>
            </div>
        );
      }
    }
    

    你可以递归地做到这一点

    【讨论】:

    • 我已将预期结果添加到我的帖子中。我不确定您的解决方案是否正确。

      标签在哪里?我喜欢重用我的 hoc 的代码。因此,当我有另一个呈现

      而不是

      的组件 MyComponent2 时,它也应该包含在 hoc 的
      标记中。

    • 我试着写一个通用的答案,你可以随时随地使用道具。当您从浏览器查看呈现的 html 时,您实际上会看到预期的答案。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签