Reusing logic with Custom Hooks
Use custom hooks if you wants specific hook Examples of possible use cases
- Fetch data
- Keep track of whether use is online
- Connect to chat room
- Pointer position
Imagine you have a useEffect with state, that you keep reusing
- Example- check if wifi connection is enabled
Well, you can reuse. Steps
- Define the functionality (using possibly
useState
anduseEffect
) - Put the code into its own function and return the state
Sample code
Rules
- Names start with “use” followd by capital letter
- Need to be pure
Notes: custom hooks don’t share state, just stateful logic (ie. wouldn’t have the same isOnline
value by default across various usages)
- Completely independent instances when used
Passing Event Handlers to Custom Hooks
Custom hooks can accept event handlers
Wrap the event handlers in useEffectEvent
to remove from the dependencies
When to use Custom Hooks With time, put most of apps effects in custom hooks Up to you where to draw the boundaries
Replace with the hooks from React when possible (consider more edge cases and whatnot)