简体   繁体   English

React Render Props 和具有功能组件的 HOC 是否有任何相关用例?

[英]Are there any relevant use case for React Render Props and HOC with functional components?

I know it's possible to use RRP and HOC patterns with functional components , my question actually is, are there any point on doing that?我知道可以将 RRP 和 HOC 模式与功能组件一起使用,我的问题实际上是,这样做有什么意义吗? Making some researches I read React/Custom Hooks can essentially handle what render props did in the past with Class Components.做一些研究我读过 React/Custom Hooks 基本上可以处理渲染道具在过去用 Class 组件所做的事情。 The fact is there is not very much information about these patterns in relation to Functional Components, almost every example out there use Class Components, so I was wondering if there's a relevant place to it in Functional Composition.事实上,关于这些模式与功能组件相关的信息并不多,几乎每个例子都使用 Class 组件,所以我想知道在功能组合中是否有相关的地方。

This is how I would put it: In most of the cases, you would use hooks;我是这样说的:在大多数情况下,你会使用钩子; in some specific situations you may use, higher-order components.在某些特定情况下,您可能会使用高阶组件。

From a control perspective, hooks provide more flexibility for the user of the code.从控制的角度来看,挂钩为代码的用户提供了更多的灵活性。 It means there is a possibility that they could be used in not-intended way.这意味着它们有可能以非预期的方式使用。 But I would call it a theoretical possibility.但我会称之为理论上的可能性。

If your reusable logic has JSX as well as some custom logic, may be providing higher-order component or, even render props, makes more sense.如果您的可重用逻辑具有 JSX 以及一些自定义逻辑,则可能提供更高阶的组件,甚至渲染道具,更有意义。 Of course this means, that you should have extremely well defined lifecycle for your component and you would not like to provide complete control to the user.当然,这意味着您应该为您的组件定义非常明确的生命周期,并且您不希望向用户提供完全控制。

An example would be react-router which is combination of both hooks and render props where applicable.一个例子是react-router ,它是适用的钩子和渲染道具的组合。

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

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