Custom React Hooks — A quick guide

Sean Marcus
3 min readDec 7, 2022

--

React hooks are a new feature in React 16.8 that lets you use state and other React features without writing a class. They were created to make it easier to share stateful logic between components and to make components that are easier to reuse.

To create a custom hook, you simply need to create a JavaScript function whose name starts with the word “use” and that calls other hooks inside of it. Here is a simple example of a custom hook that uses the useState hook to track the value of a text input:

function useTextInput(initialValue) {
const [value, setValue] = useState(initialValue);
function handleChange(event) {
setValue(event.target.value);
}
return {
value,
onChange: handleChange
};
}

In this example, the useTextInput hook returns an object with two properties: value and onChange. The value property is the current value of the input and the onChange property is a function that updates the value when the input is changed.

To use this custom hook, you would call it from inside a functional component like this:

function MyInput() {
const input = useTextInput("initial value");
return (
<input
type="text"
value={input.value}
onChange={input.onChange}
/>
);
}

In this example, the MyInput component uses the useTextInput hook to manage the state of the input and to handle changes to the input.

One of the key benefits of custom hooks is that they make it easier to share stateful logic between components. For example, if you had two components that needed to manage the value of a text input, you could use the useTextInput hook in both components, rather than having to duplicate the state management logic in each component.

Another benefit of custom hooks is that they can help you make your components more reusable. For example, if you had a custom hook for managing the value of a text input, you could use that hook in any component that needs to manage the value of a text input, without having to write the same state management logic in each component.

Overall, custom hooks are a powerful new feature in React that can help you write cleaner, more reusable code. I hope this helps! Let me know if you have any other questions.

Creating a custom hook for global state

To create a custom hook for React context, you can use the useContext hook in combination with the createContext API. Here is an example of how you might create a custom hook for a React context that manages global state:

// Create a context for the global state
const GlobalStateContext = createContext();
// Create a custom hook that uses the context
function useGlobalState() {
return useContext(GlobalStateContext);
}
// Create a provider component that wraps your app and provides the global state
function GlobalStateProvider({ children }) {
const [state, setState] = useState({ /* initial state */ });
const value = { state, setState };
return (
<GlobalStateContext.Provider value={value}>
{children}
</GlobalStateContext.Provider>
);
}

In this example, the useGlobalState hook provides an easy way to access the global state from any component in your app. To use the hook, you would call it from inside a functional component like this:

function MyComponent() {
const { state, setState } = useGlobalState();
// Use the state and setState functions to manage the global state...
}

The GlobalStateProvider component wraps your app and provides the global state to all of the components that need it. To use the provider, you would wrap your app with it like this:

ReactDOM.render(
<GlobalStateProvider>
<App />
</GlobalStateProvider>,
document.getElementById("root")
);

This is just one way to create a custom hook for a React context, but it should give you a general idea of how it works. This abstraction for context isn’t necessary since React already provides the useContext hook. This is just an example to give you ideas of using hooks.

--

--

Sean Marcus
Sean Marcus

No responses yet