Redux 重排与容器组件相同的高阶组件

Redux 重排与容器组件相同的高阶组件,redux,higher-order-components,Redux,Higher Order Components,我试着把我的头脑集中在高阶组件上,这些组件和Redux容器组件一样吗。另外,编写高阶组件容器组件的推荐方法是通过一个扩展React.Component的类,或者不使用React.Component,如redux站点中所示。有关于这个主题的文章,所以我将尝试简要解释这个概念以及它与redux的关系 你可以把HOC看作是一种增强剂或装饰剂。它接受一个现有组件并返回一个新的、改进的组件。常见的任务是:注入道具、创作、改变渲染方式等 它通常实现为一个函数:获取一个组件,生成另一个组件。根据您的目标和需要

我试着把我的头脑集中在高阶组件上,这些组件和Redux容器组件一样吗。另外,编写高阶组件容器组件的推荐方法是通过一个扩展React.Component的类,或者不使用React.Component,如redux站点中所示。

有关于这个主题的文章,所以我将尝试简要解释这个概念以及它与redux的关系

你可以把HOC看作是一种增强剂或装饰剂。它接受一个现有组件并返回一个新的、改进的组件。常见的任务是:注入道具、创作、改变渲染方式等

它通常实现为一个函数:获取一个组件,生成另一个组件。根据您的目标和需要,模式可能会有所不同

您可以扩展包装的组件:

function hoc(WrappedComponent) {
    return class HOC extends WrappedComponent {
        render() {
            return super.render();
        }
    }
}
function hoc(WrappedComponent) {
    return class HOC extends React.Component {
        render() {
            return (<WrappedComponent {...this.props} extraProp={1} />)
        }
    }
}
function hoc(WrappedComponent) {
    return (props) => (<WrappedComponent {...props} extraProp={1} />);
}
或组成包装组件:

function hoc(WrappedComponent) {
    return class HOC extends WrappedComponent {
        render() {
            return super.render();
        }
    }
}
function hoc(WrappedComponent) {
    return class HOC extends React.Component {
        render() {
            return (<WrappedComponent {...this.props} extraProp={1} />)
        }
    }
}
function hoc(WrappedComponent) {
    return (props) => (<WrappedComponent {...props} extraProp={1} />);
}
如果您希望保持简单,不需要整个生命周期,还可以使用无状态组件:

function hoc(WrappedComponent) {
    return class HOC extends WrappedComponent {
        render() {
            return super.render();
        }
    }
}
function hoc(WrappedComponent) {
    return class HOC extends React.Component {
        render() {
            return (<WrappedComponent {...this.props} extraProp={1} />)
        }
    }
}
function hoc(WrappedComponent) {
    return (props) => (<WrappedComponent {...props} extraProp={1} />);
}
我建议通过阅读加深理解

现在,这个概念没有与Redux结合,但Redux确实使用了它。 实际上是一个HOC,负责包装组件和存储注入道具之间的连接,并负责重新渲染。它接受表示组件并返回一个连接的组件。 容器连接的组件实际上是增强的组件

因此,为了明确说明:Post是一个组件,我们使用HOC connect来创建增强的组件PostContainer。

有关于这个主题的文章,所以我将尝试简要解释这个概念以及它与Redux的关系

你可以把HOC看作是一种增强剂或装饰剂。它接受一个现有组件并返回一个新的、改进的组件。常见的任务是:注入道具、创作、改变渲染方式等

它通常实现为一个函数:获取一个组件,生成另一个组件。根据您的目标和需要,模式可能会有所不同

您可以扩展包装的组件:

function hoc(WrappedComponent) {
    return class HOC extends WrappedComponent {
        render() {
            return super.render();
        }
    }
}
function hoc(WrappedComponent) {
    return class HOC extends React.Component {
        render() {
            return (<WrappedComponent {...this.props} extraProp={1} />)
        }
    }
}
function hoc(WrappedComponent) {
    return (props) => (<WrappedComponent {...props} extraProp={1} />);
}
或组成包装组件:

function hoc(WrappedComponent) {
    return class HOC extends WrappedComponent {
        render() {
            return super.render();
        }
    }
}
function hoc(WrappedComponent) {
    return class HOC extends React.Component {
        render() {
            return (<WrappedComponent {...this.props} extraProp={1} />)
        }
    }
}
function hoc(WrappedComponent) {
    return (props) => (<WrappedComponent {...props} extraProp={1} />);
}
如果您希望保持简单,不需要整个生命周期,还可以使用无状态组件:

function hoc(WrappedComponent) {
    return class HOC extends WrappedComponent {
        render() {
            return super.render();
        }
    }
}
function hoc(WrappedComponent) {
    return class HOC extends React.Component {
        render() {
            return (<WrappedComponent {...this.props} extraProp={1} />)
        }
    }
}
function hoc(WrappedComponent) {
    return (props) => (<WrappedComponent {...props} extraProp={1} />);
}
我建议通过阅读加深理解

现在,这个概念没有与Redux结合,但Redux确实使用了它。 实际上是一个HOC,负责包装组件和存储注入道具之间的连接,并负责重新渲染。它接受表示组件并返回一个连接的组件。 容器连接的组件实际上是增强的组件


因此,为了说明这一点:Post是一个组件,我们使用HOC connect来创建增强的组件PostContainer。

为了澄清这一点,似乎隐含的答案是,虽然Redux鼓励容器本身是HOC的,但如果有意义,它不应该阻止您在Redux容器之上编写HOC,对吗?这是我很好奇的事情too@aug我认为更正确的说法是容器是HOC的输出。您完全可以使用多个HOC来增强组件,不管它是否是容器。澄清一下,似乎隐含的答案是,虽然Redux鼓励容器本身是HOC的,但如果有意义,它不应该阻止您在Redux容器之上编写HOC,对吗?这是我很好奇的事情too@aug我认为更正确的说法是容器是HOC的输出。您完全可以使用多个HOC来增强组件,无论它是否为容器。