Re: Cognitive Loads in Programming #5
-
Hi, Great article, Thank You! (found it via twitter) One part, unrelated to the core topic.
Are there any references to code written is this particular style? I'm interested in the kind of trade offs that are being made, as it sounds like it's taking UI segregation seriously. Thanks to this article I also noticed your Type Enthusiast's Notes about TypeScript series. Looking forward to squeezing every little bit of wisdom out of them. Thank You again for taking the time to craft your blog! Peer |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Sorry for not replying earlier. I missed notification about your comment. Think about is this way, any hook has a type, you can just add it to input parameters in your component. E.g. a state hook const [isFocused, setFocus] = React.useState(false) has morally the type One warning, a big centralized model that has performance implications, especially if you react to keystrokes, so some distributing into local hooks is sometimes needed. This approach could lead to creation of specialized components that handle certain type of hooks keeping most components pure. E.g. <MeasurementEffect> //custom component that encapsulates some measuring and offers these to other components
{({measurements} =>
<MyInputComponent
data={data} //data handled outside
setData={setData} //modifying data handled outsize
measure={measurements.visualWidth} //custom measurement utils passed explicitly
/>
}
</MeasurementEffect> The exact types and you pass in and out depend on your design but you have higher level functions at your disposal so sky it the limit. This way, e.g. you could write tests that make your component small or big and verify how you code reacts to size but you do not encapsulate size handling and force some complex mocking to test.... I have not found much written about it, it does not mean examples are not available, I just do not know of a good source. If you start coding this way, it will become very natural to look into lenses and optics. This lists only trivial examples: Hope that helps and answers your question. |
Beta Was this translation helpful? Give feedback.
-
Thank You for your response. So apparently I misread that section of your article. Going over it again:
This left me with the impression that somehow you elected to avoid hooks entirely because their state management violated referential transparency for function components. If I understand you correctly you seem to be using a variant of the Presentational/Container component approach.
As a result hooks would only be used in non-visual container components. Now I can see how this might simplify testing of the presentational components but for the container components additional measures may be necessary as hooks depend on React's environment. Perhaps that means factoring out any container logic as pure functions that can be tested outside of React. |
Beta Was this translation helpful? Give feedback.
Sorry for not replying earlier. I missed notification about your comment.
Think about is this way, any hook has a type, you can just add it to input parameters in your component.
This allows the code to centralize the effects (hooks) into, say, the app component or other container like thing.
E.g. a state hook
has morally the type
[boolean, (b: boolean) => void]
and you could just add these to to your component and handle maintaining focus outside(focus could be maintained in a centralized model containing all data for your app, your app could have one gigantic state hook that maintains that big model).
Note this is more testable, no ne…