-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add typings for hooks #3125
Changes from all commits
6207def
0c637e5
a888200
23ee64b
a3fea9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useState, useCallback } from 'react'; | ||
|
||
export type Toggler = [ | ||
isOn: boolean, | ||
setOn: () => void, | ||
setOff: () => void, | ||
toggle: () => void, | ||
]; | ||
|
||
export interface ToggleHandlers { | ||
handleToggleOn?: () => void; | ||
handleToggleOff?: () => void; | ||
handleToggle?: (newStatus: boolean) => void; | ||
} | ||
|
||
export default function useToggle(defaultIsOn = false, handlers: ToggleHandlers = {}): Toggler { | ||
const { handleToggleOn, handleToggleOff, handleToggle } = handlers; | ||
const [isOn, setIsOn] = useState(defaultIsOn); | ||
|
||
const setOn = useCallback(() => { | ||
setIsOn(true); | ||
handleToggleOn?.(); | ||
handleToggle?.(true); | ||
}, [handleToggleOn, handleToggle]); | ||
|
||
const setOff = useCallback(() => { | ||
setIsOn(false); | ||
handleToggleOff?.(); | ||
handleToggle?.(false); | ||
}, [handleToggleOff, handleToggle]); | ||
|
||
const toggle = useCallback(() => { | ||
const doToggle = isOn ? setOff : setOn; | ||
doToggle(); | ||
}, [isOn, setOn, setOff]); | ||
|
||
return [isOn, setOn, setOff, toggle]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ const plugins = [ | |
}, | ||
}, | ||
'gatsby-plugin-react-helmet', | ||
'gatsby-plugin-typescript', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for reviewers: you may have noticed there are no |
||
{ | ||
resolve: 'gatsby-plugin-manifest', | ||
options: { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit/unrelated suggestion: could opt to disable prop-types rule for
*.test.*
files so we don't need to have these eslint disable comments throughout test files.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but I'm gonna leave that out of this PR for now.