Eliminating Props Drilling | Frontend Fundamentals #125
Replies: 1 comment 1 reply
-
Another way to eliminate props drilling and coupling is the slot pattern. This is also a kind of inversion of control (moving control further up the tree). Here's a contrived example. This pattern really becomes useful when the involved components are complex and largely unrelated. Before: function Title({ title, type }: { title: string; type: string }) {
return <h1>{type}: {title}</h1>;
}
// Bad: this component is now coupled to the Title component.
function SomethingWithTitle({ title, titleType }: { title: string, titleType: string }) {
return <div> .... <Title title={title} type={titleType} /></div>;
}
// Usage: <SomethingWithTitle title="Foo" titleType="Warning" /> After: function Title({ title, type }: { title: string; type: string }) {
return <h1>{type}: {title}</h1>;
}
// Good: Title component can change without having to change this component.
function SomethingWithTitle({ title }: { title: ReactNode }) {
return <div> .... {title}</div>;
}
// Usage: <SomethingWithTitle title={<Title title="Foo" type="Warning" />} /> |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Eliminating Props Drilling | Frontend Fundamentals
A guide for easily modifiable frontend code
https://frontend-fundamentals.com/en/code/examples/item-edit-modal.html
Beta Was this translation helpful? Give feedback.
All reactions