fix: deprecate hacky did-update

This commit is contained in:
EnixCoda 2019-11-13 14:57:03 +08:00
parent d4e4f77777
commit be6d95355c
No known key found for this signature in database
GPG key ID: FE06F5DFC1C9B8E4
2 changed files with 14 additions and 3 deletions

View file

@ -14,6 +14,7 @@ import * as React from 'react'
import { cx } from 'utils/cx'
import * as DOMHelper from 'utils/DOMHelper'
import { JSONRequest, parseURLSearch } from 'utils/general'
import { useDidUpdate } from 'utils/hooks'
import * as keyHelper from 'utils/keyHelper'
const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
@ -60,9 +61,8 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
DOMHelper.decorateGitHubPageContent({ copyFileButton, copySnippetButton })
}, [])
// reload when setting new accessToken
// special way to implement didUpdate
React.useEffect(() => () => props.init(), [accessToken])
// init again when setting new accessToken
useDidUpdate(() => props.init(), [accessToken])
const {
errorDueToAuth,

View file

@ -68,3 +68,14 @@ export function useAsyncMemo<T, D extends any[] | readonly any[]>(
}, deps)
return state.val
}
export function useDidUpdate(effect: React.EffectCallback, deps?: React.DependencyList) {
const firstTime = React.useRef(true)
React.useEffect(() => {
if (firstTime.current) {
firstTime.current = false
return
}
return effect()
}, deps)
}