Creating Typed Custom Hooks
When building reusable logic in React, you often create custom hooks. Typing these hooks with TypeScript ensures that both their parameters and return values are used safely, making them robust and easy to maintain. To see how this works in practice, consider a custom hook that manages a boolean toggle state. You want to type both the initial value parameter and the returned state and toggle function.
Begin by defining the hook's parameter and return types. The parameter, initial, should be a boolean. The hook will return a tuple: the current boolean state and a function to toggle it. Here is a step-by-step example of how you might write and type such a custom hook:
import { useState } from "react";
function useToggle(initial: boolean): [boolean, () => void] {
const [value, setValue] = useState<boolean>(initial);
const toggle = () => setValue((v) => !v);
return [value, toggle];
}
// Usage
const [isOpen, toggleOpen] = useToggle(false);
In this example, the useToggle hook takes an initial boolean value. The return type is a tuple: the current value (a boolean) and a function that toggles that value. By specifying these types, TypeScript will warn you if you use the hook incorrectly, such as passing a non-boolean initial value or misusing the returned function.
1. What is the main reason to type custom hooks in React?
2. What is a common pitfall when typing custom hooks?
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you show more examples of custom hooks with TypeScript?
How can I extend this hook to accept an optional initial value?
What are some best practices for typing custom hooks in TypeScript?
Awesome!
Completion rate improved to 4.17
Creating Typed Custom Hooks
Veeg om het menu te tonen
When building reusable logic in React, you often create custom hooks. Typing these hooks with TypeScript ensures that both their parameters and return values are used safely, making them robust and easy to maintain. To see how this works in practice, consider a custom hook that manages a boolean toggle state. You want to type both the initial value parameter and the returned state and toggle function.
Begin by defining the hook's parameter and return types. The parameter, initial, should be a boolean. The hook will return a tuple: the current boolean state and a function to toggle it. Here is a step-by-step example of how you might write and type such a custom hook:
import { useState } from "react";
function useToggle(initial: boolean): [boolean, () => void] {
const [value, setValue] = useState<boolean>(initial);
const toggle = () => setValue((v) => !v);
return [value, toggle];
}
// Usage
const [isOpen, toggleOpen] = useToggle(false);
In this example, the useToggle hook takes an initial boolean value. The return type is a tuple: the current value (a boolean) and a function that toggles that value. By specifying these types, TypeScript will warn you if you use the hook incorrectly, such as passing a non-boolean initial value or misusing the returned function.
1. What is the main reason to type custom hooks in React?
2. What is a common pitfall when typing custom hooks?
Bedankt voor je feedback!