Skip to content

Component Patterns: Render prop vs. hook #4486

Closed Answered by sebelga
thompsongl asked this question in General
Discussion options

You must be logged in to vote

Got pinged by @cjcenizal on this so I'll bring my 2 cents 😊

I am not a huge fan of hooks to return components. So not option 3.
Option 4 seems a bit over-engineered to me.

So that leaves us with the render props pattern. I wonder why we need to pass down the component like that. What about using context for this? And, if needed, expose a hook to access certain props (e.g. isPanelOpen)?

// context.ts

const context = React.createContext();

export const Provider = () => {
  // Here comes all the different state and handler to pass down
  
  return (
    <context.Provider value={{ /* state and handlers */}}>{children}</context.Provider>
  )
}

export Consumer = context.Consumer;

// Let con…

Replies: 4 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@cchaos
Comment options

@sebelga
Comment options

@cchaos
Comment options

@sebelga
Comment options

Answer selected by cee-chen
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants