Skip to content

Commit 901544d

Browse files
committed
Maintain same number of hooks between renders
1 parent e66d825 commit 901544d

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

theme/src/components/PreviousNext.tsx

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { useContext } from 'react'
1+
import { Button, Divider, Flex, Icons } from '@committed/components'
2+
import React, { useCallback, useContext } from 'react'
3+
import { useHotkeys } from 'react-hotkeys-hook'
24
import { Info } from '../types'
35
import { DocsContext } from './Layout'
4-
import { useHotkeys } from 'react-hotkeys-hook'
5-
import { Flex, Button, Divider, Icons } from '@committed/components'
66

77
export interface PreviousNextProps {
88
previous?: Info
@@ -19,15 +19,22 @@ export const PreviousNext = ({ previous, next }: PreviousNextProps) => {
1919
justifyContent = 'flex-start'
2020
}
2121

22-
if (previous) {
23-
useHotkeys('left', () => navigate(previous.url))
24-
useHotkeys('shift+left', () => navigate(previous.url))
25-
}
22+
const navigateToPrevious = useCallback(() => {
23+
if (previous) {
24+
navigate(previous.url)
25+
}
26+
}, [previous, navigate])
2627

27-
if (next) {
28-
useHotkeys('right', () => navigate(next.url))
29-
useHotkeys('shift+right', () => navigate(next.url))
30-
}
28+
const navigateToNext = useCallback(() => {
29+
if (next) {
30+
navigate(next.url)
31+
}
32+
}, [navigate, next])
33+
34+
useHotkeys('left', navigateToPrevious, [navigateToPrevious])
35+
useHotkeys('shift+left', navigateToPrevious, [navigateToPrevious])
36+
useHotkeys('right', navigateToNext, [navigateToNext])
37+
useHotkeys('shift+right', navigateToNext, [navigateToNext])
3138

3239
return (
3340
<>

theme/src/components/Tree.tsx

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React, { useContext } from 'react'
2-
import { Item } from '../types'
3-
import TreeNode from './TreeNode'
4-
import { DocsContext } from './Layout'
1+
import { List } from '@committed/components'
52
import { withPrefix } from 'gatsby'
6-
import { firstUrl, firstInfo } from '../util/tree'
3+
import React, { useCallback, useContext } from 'react'
74
import { useHotkeys } from 'react-hotkeys-hook'
8-
import { List } from '@committed/components'
5+
import { Item } from '../types'
6+
import { firstInfo, firstUrl } from '../util/tree'
7+
import { DocsContext } from './Layout'
8+
import TreeNode from './TreeNode'
99

1010
export interface TreeProps {
1111
location: any
@@ -42,23 +42,28 @@ export const Tree = ({
4242

4343
const index = data.items.findIndex(item => isParent(item))
4444

45-
if (index == -1) {
46-
useHotkeys('shift+down', () => navigateItem(0))
47-
} else {
48-
if (index == 0) {
49-
useHotkeys('shift+up', () => navigate('/'))
45+
const navigateDown = useCallback(() => {
46+
if (index === -1) {
47+
navigateItem(0)
48+
} else if (index < data.items.length - 1) {
49+
navigateItem(index + 1)
5050
}
51-
if (index > 0) {
51+
}, [navigateItem, index, data])
52+
53+
const navigateUp = useCallback(() => {
54+
if (index === 0) {
55+
navigate('/')
56+
} else if (index > 0) {
5257
if (isActive(firstInfo(data.items[index]).id)) {
53-
useHotkeys('shift+up', () => navigateItem(index - 1))
58+
navigateItem(index - 1)
5459
} else {
55-
useHotkeys('shift+up', () => navigateItem(index))
60+
navigateItem(index)
5661
}
5762
}
58-
if (index < data.items.length - 1) {
59-
useHotkeys('shift+down', () => navigateItem(index + 1))
60-
}
61-
}
63+
}, [navigateItem, index, data, firstInfo, isActive])
64+
65+
useHotkeys('shift+down', navigateDown, [navigateDown])
66+
useHotkeys('shift+up', navigateUp, [navigateUp])
6267

6368
return (
6469
<List dense>

theme/src/print/Header.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { Column } from '@committed/components'
2+
import { graphql, useStaticQuery } from 'gatsby'
13
import React from 'react'
2-
import { useStaticQuery, graphql, navigate } from 'gatsby'
3-
import { useHotkeys } from 'react-hotkeys-hook'
44
import { Classification } from './Classification'
5-
import { Column } from '@committed/components'
65

76
export const Header = () => {
87
const {

0 commit comments

Comments
 (0)