@@ -37,15 +37,15 @@ import { useCallback } from 'react'
3737
3838// Only uses useCallback
3939function useCallbacks () {
40- const handleClick = useCallback (() => {
41- console .log (' clicked' )
42- }, [])
40+ const handleClick = useCallback (() => {
41+ console .log (' clicked' )
42+ }, [])
4343
44- const handleSubmit = useCallback (() => {
45- console .log (' submitted' )
46- }, [])
44+ const handleSubmit = useCallback (() => {
45+ console .log (' submitted' )
46+ }, [])
4747
48- return { handleClick, handleSubmit }
48+ return { handleClick, handleSubmit }
4949}
5050```
5151
@@ -54,11 +54,11 @@ import * as React from 'react'
5454
5555// Only uses useMemo
5656function useMemoValues () {
57- const expensiveValue = React .useMemo (() => {
58- return calculateSomething ()
59- }, [])
57+ const expensiveValue = React .useMemo (() => {
58+ return calculateSomething ()
59+ }, [])
6060
61- return expensiveValue
61+ return expensiveValue
6262}
6363```
6464
@@ -67,9 +67,9 @@ import React from 'react'
6767
6868// Uses both useCallback and useMemo but no other hooks
6969function useMixed () {
70- const callback = React .useCallback (() => {}, [])
71- const value = React .useMemo (() => ({}), [])
72- return { callback, value }
70+ const callback = React .useCallback (() => {}, [])
71+ const value = React .useMemo (() => ({}), [])
72+ return { callback, value }
7373}
7474```
7575
@@ -78,9 +78,9 @@ function useMixed() {
7878``` jsx
7979// Calls no hooks at all (see top of file for note about this example)
8080function useUtilities () {
81- const format = (text ) => text .toUpperCase ()
82- const process = (data ) => data .map (x => x * 2 )
83- return { format, process }
81+ const format = (text ) => text .toUpperCase ()
82+ const process = (data ) => data .map (x => x * 2 )
83+ return { format, process }
8484}
8585```
8686
@@ -89,9 +89,9 @@ import { useState, useCallback } from 'react'
8989
9090// Uses other hooks (useState) - React Compiler can optimize this
9191function useCounter () {
92- const [count , setCount ] = useState (0 )
93- const increment = useCallback (() => setCount (c => c + 1 ), [])
94- return { count, increment }
92+ const [count , setCount ] = useState (0 )
93+ const increment = useCallback (() => setCount (c => c + 1 ), [])
94+ return { count, increment }
9595}
9696```
9797
@@ -100,19 +100,19 @@ import { useState, useMemo, useEffect } from 'react'
100100
101101// Uses useEffect - React Compiler can optimize this
102102function useWindowSize () {
103- const [size , setSize ] = useState ({ width: 0 , height: 0 })
104-
105- useEffect (() => {
106- const handler = () => setSize ({
107- width: window .innerWidth ,
108- height: window .innerHeight
109- })
110- window .addEventListener (' resize' , handler)
111- return () => window .removeEventListener (' resize' , handler)
112- }, [])
113-
114- const memoizedSize = useMemo (() => size, [size])
115- return memoizedSize
103+ const [size , setSize ] = useState ({ width: 0 , height: 0 })
104+
105+ useEffect (() => {
106+ const handler = () => setSize ({
107+ width: window .innerWidth ,
108+ height: window .innerHeight
109+ })
110+ window .addEventListener (' resize' , handler)
111+ return () => window .removeEventListener (' resize' , handler)
112+ }, [])
113+
114+ const memoizedSize = useMemo (() => size, [size])
115+ return memoizedSize
116116}
117117```
118118
@@ -121,11 +121,11 @@ import { useCallback } from 'react'
121121
122122// Contains JSX - different use case
123123function useButton () {
124- const handleClick = useCallback (() => {
125- console .log (' clicked' )
126- }, [])
124+ const handleClick = useCallback (() => {
125+ console .log (' clicked' )
126+ }, [])
127127
128- return < button onClick= {handleClick}> Click me< / button>
128+ return < button onClick= {handleClick}> Click me< / button>
129129}
130130```
131131
@@ -137,9 +137,9 @@ function useButton() {
137137``` jsx
138138import { useCallback } from ' react'
139139function createHelpers () {
140- const helper1 = useCallback (() => {}, [])
141- const helper2 = useMemo (() => ({}), [])
142- return { helper1, helper2 }
140+ const helper1 = useCallback (() => {}, [])
141+ const helper2 = useMemo (() => ({}), [])
142+ return { helper1, helper2 }
143143}
144144```
145145
0 commit comments