Skip to content

Instantly share code, notes, and snippets.

@kt3k
Last active August 7, 2020 09:21
Show Gist options
  • Save kt3k/0013ff70e0a0c05a8b18e5f302e42868 to your computer and use it in GitHub Desktop.
Save kt3k/0013ff70e0a0c05a8b18e5f302e42868 to your computer and use it in GitHub Desktop.
doIf
import { useEffect } from 'react'
export const doIf = (cond: boolean, effect: () => unknown): void => {
useEffect(() => {
if (cond) {
effect()
}
}, [cond])
}
import React from 'react'
export const SomeComponent: React.FC = () => {
const { isLoggedIn } = useAuthState()
doIf(isLoggedIn, () => {
setClient(client(...withAuthInfo...))
})
return ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment