Why use this over extracting components? #2
Replies: 2 comments 1 reply
-
|
Following |
Beta Was this translation helpful? Give feedback.
-
|
Hey, Kevin. This package specifically exists to allow developers to include nested state where they see fit. I am not officially suggesting this practice. In fact, in many cases, it makes the most sense to break a component out into multiple components for reusability, as you pointed out. With that said, there are use cases where a package like this would be valuable— you may want to use a hook conditionally simply to wrap a very small piece of DOM state, like a single element, or to include state when iterating over an array that doesn't need to be reused. These are great use cases for RenderHooks or a similar package. I wasn't surprised to find, after developing RenderHooks, that there are several other packages in the same problem space striving to solve the problem of nested component syntax. For this reason, the Meta team has been actively inquiring about the reasoning behind such packages to see if there are any affordances that can be made within React core to solve this issue in a better way. You're not wrong to voice your concerns about the React compiler; they are valid. In most cases, the React compiler will have no issue compiling code that uses RenderHooks. However, RenderHooks will may prove problematic in certain circumstances, especially when rendering different content conditionally or when nested recursively. For this reason, I am actively working on an overhaul of this package as a build tool plugin that will work regardless of your bundler and simplify the syntax greatly. Before — ❌ extra syntax, render props, not compatible with React Compiler const SimpleCounters = () => (
<ul>
{items.map((item) => (
<$ key={item.id}>
{() => {
const [name, setName] = React.useState(item.name);
// more logic ...
return <button onClick={() => setName(newName)}>{newName}</button>;
}}
</$>
))}
</ul>
);After — ✅ simpler syntax (traditionally invalid, but corrected at build time), fully compatible with React Compiler const SimpleCounters = () => (
<ul>
{items.map((item) => {
const [name, setName] = useState(item.name);
// more logic ...
return <button key={item.id} onClick={() => setName(newName)}>{newName}</button>;
})}
</ul>
); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Why should this library be used instead of simply refactoring component into smaller and more reusable ones, that the React Compiler will probably have a better time optimizing them? Is there any benefit other that prop drilling that can often be solved by refactoring or the use of contexts?
I am not arguing in any way on the architecture of the library, the project itself or on the well written documentation, but more on the need that this library is trying to fill in.
Here is your example of nested hooks, quickly refactored in a way that would allow you to reuse the
Postcomponent as well as theCatcomponent.Beta Was this translation helpful? Give feedback.
All reactions