pmndrs / react-nil

⃝ A react null renderer
MIT License
793 stars 14 forks source link

Use custom hooks instead #3

Closed jdnichollsc closed 4 years ago

jdnichollsc commented 4 years ago

Logic without render... that's a custom hook! it doesn't make sense :)

drcmda commented 4 years ago

yes of course you can use custom hooks, thats the point. a component gives hooks context, they couldn't be without a lifecycle. components that render nothing are very common the react space, for years. this is stretching it though, im aware of that. 😄

jdnichollsc commented 4 years ago

Can you provide an example of components that render nothing? Also with Unit tests to validate the expected behavior? I mean, with hooks you can validate that with tools like: https://github.com/testing-library/react-hooks-testing-library Just to understand your real world situation 😄

JohannesKlauss commented 4 years ago

Hooks depend on a component being mounted. There are indeed real world situations for this. For example your app could have a bulk of side effect logic just managing logging web sockets, observing network calls, etc.

You could of course throw that in a custom hook and include it i.e. in the main App component but does it really belong there? A hook that logs something client <-> server or client <-> client doesn't really belong in a UI graph. So for that case I'd use this renderer, because that hook logic should live independent of the actual UI.

Also mind you that "Logic without render" as you put it in your first comment is not a hook. A hook always is attached to some kind of component and therefore a render.

jdnichollsc commented 4 years ago

For logging is better using Epics (with Redux, etc) or also just services, using Factory pattern, etc. You can use Websockets from hooks without problems and also reuse the same connection using Singleton pattern from services, etc instead of having components that are not UI related, being affected by the render tree of react...

JohannesKlauss commented 4 years ago

Well that's the whole point. UI non related things that shouldn't be affected by the UI render tree, but you still want to build it up as a Graph with all the lifecycle React provides. Using web audio is an almost perfect use case for that. There is no UI involved in connect audio nodes, but it's still a graph and this library enables a reduction of complexity in that area that is unheard of.

jdnichollsc commented 4 years ago

I can't see a problem to use custom Hooks (also with High Order Components) instead from your screens, but as you want :)