'Reusing Logic with Custom Hooks'·续3
label, button { display: block; margin-bottom: 20px; }
html, body { min-height: 300px; }
.welcome {
opacity: 0;
color: white;
padding: 50px;
text-align: center;
font-size: 50px;
background-image: radial-gradient(circle, rgba(63,94,251,1) 0%, rgba(252,70,107,1) 100%);
}
Effects let you connect React to external systems. The more coordination between Effects is needed (for example, to chain multiple animations), the more it makes sense to extract that logic out of Effects and Hooks completely like in the sandbox above. Then, the code you extracted becomes the "external system". This lets your Effects stay simple because they only need to send messages to the system you've moved outside React.
The examples above assume that the fade-in logic needs to be written in JavaScript. However, this particular fade-in animation is both simpler and much more efficient to implement with a plain CSS Animation:
import { useState, useEffect, useRef } from 'react';
import './welcome.css';
function Welcome() {
return (
<h1 className="welcome">
Welcome
</h1>
);
}
export default function App() {
const [show, setShow] = useState(false);
return (
<>
<button onClick={() => setShow(!show)}>
{show ? 'Remove' : 'Show'}
</button>
<hr />
{show && <Welcome />}
</>
);
}
```css src/styles.css label, button { display: block; margin-bottom: 20px; } html, body { min-height: 300px; }
```css src/welcome.css active
.welcome {
color: white;
padding: 50px;
text-align: center;
font-size: 50px;
background-image: radial-gradient(circle, rgba(63,94,251,1) 0%, rgba(252,70,107,1) 100%);
animation: fadeIn 1000ms;
}
@keyframes fadeIn {
0% { opacity: 0; }
100% { opacity: 1; }
}
Sometimes, you don't even need a Hook!
- Custom Hooks let you share logic between components.
- Custom Hooks must be named starting with
usefollowed by a capital letter. - Custom Hooks only share stateful logic, not state itself.
- You can pass reactive values from one Hook to another, and they stay up-to-date.
- All Hooks re-run every time your component re-renders.
- The code of your custom Hooks should be pure, like your component's code.
- Wrap event handlers received by custom Hooks into Effect Events.
- Don't create custom Hooks like
useMount. Keep their purpose specific. - It's up to you how and where to choose the boundaries of your code.
Extract a useCounter Hook
This component uses a state variable and an Effect to display a number that increments every second. Extract this logic into a custom Hook called useCounter. Your goal is to make the Counter component implementation look exactly like this:
export default function Counter() {
const count = useCounter();
return <h1>Seconds passed: {count}</h1>;
}
You'll need to write your custom Hook in useCounter.js and import it into the App.js file.
import { useState, useEffect } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => {
setCount(c => c + 1);
}, 1000);
return () => clearInterval(id);
}, []);
return <h1>Seconds passed: {count}</h1>;
}
```js src/useCounter.js // Write your custom Hook in this file!
Your code should look like this:
```js
import { useCounter } from './useCounter.js';
export default function Counter() {
const count = useCounter();
return <h1>Seconds passed: {count}</h1>;
}
```js src/useCounter.js import { useState, useEffect } from 'react';
export function useCounter() { const [count, setCount] = useState(0); useEffect(() => { const id = setInterval(() => { setCount(c => c + 1); }, 1000); return () => clearInterval(id); }, []); return count; }
Notice that `App.js` doesn't need to import `useState` or `useEffect` anymore.
#### Make the counter delay configurable
In this example, there is a `delay` state variable controlled by a slider, but its value is not used. Pass the `delay` value to your custom `useCounter` Hook, and change the `useCounter` Hook to use the passed `delay` instead of hardcoding `1000` ms.
```js
import { useState } from 'react';
import { useCounter } from './useCounter.js';
export default function Counter() {
const [delay, setDelay] = useState(1000);
const count = useCounter();
return (
<>
<label>
Tick duration: {delay} ms
<br />
<input
type="range"
value={delay}
min="10"
max="2000"
onChange={e => setDelay(Number(e.target.value))}
/>
</label>
<hr />
<h1>Ticks: {count}</h1>
</>
);
}
```js src/useCounter.js import { useState, useEffect } from 'react';
export function useCounter() { const [count, setCount] = useState(0); useEffect(() => { const id = setInterval(() => { setCount(c => c + 1); }, 1000); return () => clearInterval(id); }, []); return count; }
Pass the `delay` to your Hook with `useCounter(delay)`. Then, inside the Hook, use `delay` instead of the hardcoded `1000` value. You'll need to add `delay` to your Effect's dependencies. This ensures that a change in `delay` will reset the interval.
```js
import { useState } from 'react';
import { useCounter } from './useCounter.js';
export default function Counter() {
const [delay, setDelay] = useState(1000);
const count = useCounter(delay);
return (
<>
<label>
Tick duration: {delay} ms
<br />
<input
type="range"
value={delay}
min="10"
max="2000"
onChange={e => setDelay(Number(e.target.value))}
/>
</label>
<hr />
<h1>Ticks: {count}</h1>
</>
);
}
```js src/useCounter.js import { useState, useEffect } from 'react';
export function useCounter(delay) { const [count, setCount] = useState(0); useEffect(() => { const id = setInterval(() => { setCount(c => c + 1); }, delay); return () => clearInterval(id); }, [delay]); return count; }
#### Extract `useInterval` out of `useCounter`
Currently, your `useCounter` Hook does two things. It sets up an interval, and it also increments a state variable on every interval tick. Split out the logic that sets up the interval into a separate Hook called `useInterval`. It should take two arguments: the `onTick` callback, and the `delay`. After this change, your `useCounter` implementation should look like this:
```js
export function useCounter(delay) {
const [count, setCount] = useState(0);
useInterval(() => {
setCount(c => c + 1);
}, delay);
return count;
}
Write useInterval in the useInterval.js file and import it into the useCounter.js file.
import { useCounter } from './useCounter.js';
export default function Counter() {
const count = useCounter(1000);
return <h1>Seconds passed: {count}</h1>;
}
```js src/useCounter.js import { useState, useEffect } from 'react';
export function useCounter(delay) { const [count, setCount] = useState(0); useEffect(() => { const id = setInterval(() => { setCount(c => c + 1); }, delay); return () => clearInterval(id); }, [delay]); return count; }
```js src/useInterval.js
// Write your Hook here!
The logic inside useInterval should set up and clear the interval. It doesn't need to do anything else.
import { useCounter } from './useCounter.js';
export default function Counter() {
const count = useCounter(1000);
return <h1>Seconds passed: {count}</h1>;
}
```js src/useCounter.js import { useState } from 'react'; import { useInterval } from './useInterval.js';
export function useCounter(delay) { const [count, setCount] = useState(0); useInterval(() => { setCount(c => c + 1); }, delay); return count; }
```js src/useInterval.js active
import { useEffect } from 'react';
export function useInterval(onTick, delay) {
useEffect(() => {
const id = setInterval(onTick, delay);
return () => clearInterval(id);
}, [onTick, delay]);
}
Note that there is a bit of a problem with this solution, which you'll solve in the next challenge.
Fix a resetting interval
In this example, there are two separate intervals.
The App component calls useCounter, which calls useInterval to update the counter every second. But the App component also calls useInterval to randomly update the page background color every two seconds.
For some reason, the callback that updates the page background never runs. Add some logs inside useInterval:
```js {2,5} useEffect(() => { console.log('✅ Setting up an interval with delay ', delay) const id = setInterval(onTick, delay); return () => { console.log('❌ Clearing an interval with delay ', delay) clearInterval(id); }; }, [onTick, delay]);
Do the logs match what you expect to happen? If some of your Effects seem to re-synchronize unnecessarily, can you guess which dependency is causing that to happen? Is there some way to [remove that dependency](/learn/removing-effect-dependencies) from your Effect?
After you fix the issue, you should expect the page background to update every two seconds.
It looks like your `useInterval` Hook accepts an event listener as an argument. Can you think of some way to wrap that event listener so that it doesn't need to be a dependency of your Effect?
```js
import { useCounter } from './useCounter.js';
import { useInterval } from './useInterval.js';
export default function Counter() {
const count = useCounter(1000);
useInterval(() => {
const randomColor = `hsla(${Math.random() * 360}, 100%, 50%, 0.2)`;
document.body.style.backgroundColor = randomColor;
}, 2000);
return <h1>Seconds passed: {count}</h1>;
}
```js src/useCounter.js import { useState } from 'react'; import { useInterval } from './useInterval.js';
export function useCounter(delay) { const [count, setCount] = useState(0); useInterval(() => { setCount(c => c + 1); }, delay); return count; }
```js src/useInterval.js
import { useEffect } from 'react';
import { useEffectEvent } from 'react';
export function useInterval(onTick, delay) {
useEffect(() => {
const id = setInterval(onTick, delay);
return () => {
clearInterval(id);
};
}, [onTick, delay]);
}
Inside useInterval, wrap the tick callback into an Effect Event, as you did earlier on this page.
This will allow you to omit onTick from dependencies of your Effect. The Effect won't re-synchronize on every re-render of the component, so the page background color change interval won't get reset every second before it has a chance to fire.
With this change, both intervals work as expected and don't interfere with each other:
import { useCounter } from './useCounter.js';
import { useInterval } from './useInterval.js';
export default function Counter() {
const count = useCounter(1000);
useInterval(() => {
const randomColor = `hsla(${Math.random() * 360}, 100%, 50%, 0.2)`;
document.body.style.backgroundColor = randomColor;
}, 2000);
return <h1>Seconds passed: {count}</h1>;
}
```js src/useCounter.js import { useState } from 'react'; import { useInterval } from './useInterval.js';
export function useCounter(delay) { const [count, setCount] = useState(0); useInterval(() => { setCount(c => c + 1); }, delay); return count; }
```js src/useInterval.js active
import { useEffect } from 'react';
import { useEffectEvent } from 'react';
export function useInterval(callback, delay) {
const onTick = useEffectEvent(callback);
useEffect(() => {
const id = setInterval(onTick, delay);
return () => clearInterval(id);
}, [delay]);
}
Implement a staggering movement
In this example, the usePointerPosition() Hook tracks the current pointer position. Try moving your cursor or your finger over the preview area and see the red dot follow your movement. Its position is saved in the pos1 variable.
In fact, there are five (!) different red dots being rendered. You don't see them because currently they all appear at the same position. This is what you need to fix. What you want to implement instead is a "staggered" movement: each dot should "follow" the previous dot's path. For example, if you quickly move your cursor, the first dot should follow it immediately, the second dot should follow the first dot with a small delay, the third dot should follow the second dot, and so on.
You need to implement the useDelayedValue custom Hook. Its current implementation returns the value provided to it. Instead, you want to return the value back from delay milliseconds ago. You might need some state and an Effect to do this.
After you implement useDelayedValue, you should see the dots move following one another.
You'll need to store the delayedValue as a state variable inside your custom Hook. When the value changes, you'll want to run an Effect. This Effect should update delayedValue after the delay. You might find it helpful to call setTimeout.
Does this Effect need cleanup? Why or why not?
import { usePointerPosition } from './usePointerPosition.js';
function useDelayedValue(value, delay) {
// TODO: Implement this Hook
return value;
}
export default function Canvas() {
const pos1 = usePointerPosition();
const pos2 = useDelayedValue(pos1, 100);
const pos3 = useDelayedValue(pos2, 200);
const pos4 = useDelayedValue(pos3, 100);
const pos5 = useDelayedValue(pos3, 50);
return (
<>
<Dot position={pos1} opacity={1} />
<Dot position={pos2} opacity={0.8} />
<Dot position={pos3} opacity={0.6} />
<Dot position={pos4} opacity={0.4} />
<Dot position={pos5} opacity={0.2} />
</>
);
}
function Dot({ position, opacity }) {
return (
<div style={{
position: 'absolute',
backgroundColor: 'pink',
borderRadius: '50%',
opacity,
transform: `translate(${position.x}px, ${position.y}px)`,
pointerEvents: 'none',
left: -20,
top: -20,
width: 40,
height: 40,
}} />
);
}
```js src/usePointerPosition.js import { useState, useEffect } from 'react';
export function usePointerPosition() { const [position, setPosition] = useState({ x: 0, y: 0 }); useEffect(() => { function handleMove(e) { setPosition({ x: e.clientX, y: e.clientY }); } window.addEventListener('pointermove', handleMove); return () => window.removeEventListener('pointermove', handleMove); }, []); return position; }
```css
body { min-height: 300px; }
Here is a working version. You keep the delayedValue as a state variable. When value updates, your Effect schedules a timeout to update the delayedValue. This is why the delayedValue always "lags behind" the actual value.
import { useState, useEffect } from 'react';
import { usePointerPosition } from './usePointerPosition.js';
function useDelayedValue(value, delay) {
const [delayedValue, setDelayedValue] = useState(value);
useEffect(() => {
setTimeout(() => {
setDelayedValue(value);
}, delay);
}, [value, delay]);
return delayedValue;
}
export default function Canvas() {
const pos1 = usePointerPosition();
const pos2 = useDelayedValue(pos1, 100);
const pos3 = useDelayedValue(pos2, 200);
const pos4 = useDelayedValue(pos3, 100);
const pos5 = useDelayedValue(pos3, 50);
return (
<>
<Dot position={pos1} opacity={1} />
<Dot position={pos2} opacity={0.8} />
<Dot position={pos3} opacity={0.6} />
<Dot position={pos4} opacity={0.4} />
<Dot position={pos5} opacity={0.2} />
</>
);
}
function Dot({ position, opacity }) {
return (
<div style={{
position: 'absolute',
backgroundColor: 'pink',
borderRadius: '50%',
opacity,
transform: `translate(${position.x}px, ${position.y}px)`,
pointerEvents: 'none',
left: -20,
top: -20,
width: 40,
height: 40,
}} />
);
}
```js src/usePointerPosition.js import { useState, useEffect } from 'react';
export function usePointerPosition() { const [position, setPosition] = useState({ x: 0, y: 0 }); useEffect(() => { function handleMove(e) { setPosition({ x: e.clientX, y: e.clientY }); } window.addEventListener('pointermove', handleMove); return () => window.removeEventListener('pointermove', handleMove); }, []); return position; }
```css
body { min-height: 300px; }
Note that this Effect does not need cleanup. If you called clearTimeout in the cleanup function, then each time the value changes, it would reset the already scheduled timeout. To keep the movement continuous, you want all the timeouts to fire.
