简体   繁体   English

渲染道具与 HOC?

[英]Render Props vs HOC?

I'm trying to learn React from scratch and having a deep knowledge of concepts !我正在尝试从头开始学习 React 并对概念有深入的了解!

Today I was searching about HOC, Render Props and the differences between the two.今天我在搜索 HOC、Render Props 以及两者之间的区别。 I've checked render times for both.我已经检查了两者的渲染时间。 I wrote a console.log('rendered') into render to check render times in browser console.我在 render 中写了一个console.log('rendered')来检查浏览器控制台中的渲染时间。

HOC: When I used HOC to write an usable component, I saw after each changes on props I've render for HOC and component that used HOC. HOC:当我使用 HOC 编写一个可用的组件时,我看到在我为 HOC 和使用 HOC 的组件渲染的道具上的每次更改之后。

Render Prop: In this case I've changed the props, but only wrapper component has rendered.渲染道具:在这种情况下,我更改了道具,但只渲染了包装器组件。 because with render props we load only one component and inject codes to use that component feature !因为使用渲染道具我们只加载一个组件并注入代码以使用该组件功能!

So, Is it a benefit to use Render Props instead HOC components?那么,使用 Render Props 代替 HOC 组件有好处吗? Or HOC components are usable and powerful yet?还是 HOC 组件可用且功能强大?

Thanks谢谢

HOC , Render Props and now hooks all serve to the same purpose: Share stateful logic between components . HOCRender Props和 now hooks都服务于同一个目的:在组件之间共享有状态的逻辑 There is actually no way to tell which one is better or worst.实际上没有办法判断哪个更好或更差。 All depends on your use case.一切都取决于您的用例。


High Order Components are composable. High Order Components是可组合的。 It's easy to nest them嵌套它们很容易

const withProps = (Component) => connect(mapState, mapDispatch)(<Component foo='bar' />)

Children as a function is a bad pattern for composability, nesting looks a lot like a callback hell cause they need to be executed inside an jsx block作为 function 的子级是可组合性不好的模式,嵌套看起来很像callback地狱,因为它们需要在jsx块内执行

const Component = () =>{
    return(
         <Consumer>
             {
               props =>(
                   <ThemeConsumer>
                       {
                           theme => <Child {...props} {...theme} />
                       }
                   </ThemeConsumer>              
               )                  
              }
         </Consumer>
     )
}

On the other hand, render props it's easy to set up, have less boilerplate and in most cases are easier to reason about.另一方面, render props很容易设置,样板更少,并且在大多数情况下更容易推理。


Hooks bring the best of both worlds钩子带来两全其美

hooks are composable, can be easily nested, and are simple to reason about cause after all they're just plain old functions hooks是可组合的,可以轻松嵌套,并且很容易推理原因毕竟它们只是普通的旧functions

const useConfig = () =>{
    const customProps = useCustomProps()
    const theme = useContext(ThemeContext)

    return [customProps, theme]
}

const Component = () =>{
    const [props, theme] = useConfig() 
}

But again: There is no such thing as the best pattern.但同样:没有最好的模式。 It's just a matter of where are you going to use it.这只是你要在哪里使用它的问题。

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

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