🔰 We'll change the screens with useStepper hook
Prelude
Let's say you have a simple screen with steps. It can look more or less like the one in the gif below:
Loading
An example of usage in an application
We'll write a useStepper hook that will have the basic functionality to change the step and will protect us from potential bugs.
In this hook we'll encapsulate common logic for changing steps, so later we don't have to repeat ourselves and we'll make it type-safety.
We want to avoid:
Using wrong key
Passing invalid props to component
Moving beyond the range of steps
Displaying the wrong step
Making a mistake in navigation logic
If you interested about how it was created you can check this article.
The usage of useStepper
For a start, look at how we will use our hook. Pay attention to the hints that appear thanks to the TypeScript.
Loading
Type safety and protection from passing not allowed props
Loading
The implementation of useStepper
Loading
Loading
Loading
Loading
Full example
At this link you have the end result. Types, tests, implementation and re-exports.
Summary
Now you know how to handle the steps on the user interface in an interesting way. The created solution protects against many basic mistakes that can be made when implementing such functionality.
It is worth noting that the logic itself is simple - the complexity is introduced by TypeScript (as always). So it is up to you whether you want to use this solution with or without TS.
If you enjoyed it, be sure to visit us on Linkedin where we regularly upload content from programming.If you want to understand the process of creating useStepper hook - you can check this article.
- 1. Rendering
12 minutes
Creating portals with custom usePortal hook
2 m
We'll change the screens with useStepper hook
3 m
Manage components appearance with useToggle hook
4 m
Removing server warnings for useLayoutEffect with custom hook
3 m
- 2. Forms
4 minutes
- 3. Events
23 minutes
useClipboard
4 m
useElementSize
4 m
Deep dive into useIntersectionObserver hook
4 m
Read the scroll metadata and direction with useScroll hook
5 m
Detect outside click with the useClickOutside hook
6 m
- 4. Guards
5 minutes
- 5. Interactions
5 minutes