Skip to content

Instantly share code, notes, and snippets.

View d-johnston's full-sized avatar
🦔

Dylan Johnston d-johnston

🦔
View GitHub Profile
@d-johnston
d-johnston / useFocusEffect example.ts
Created June 12, 2025 18:19
React Navigation (for React Native) showing their useFocusEffect which stops a counting mechanism when the page focus is lost
const [count, setCount] = useState(0)
const [slowCount, setSlowCount] = useState(0)
const [isToggled, setIsToggled] = useState(false)
useFocusEffect(
useCallback(() => {
console.log('slowCount useEffect ran')
const interval = setInterval(() => {
console.log('slow count incremented')
@d-johnston
d-johnston / effect snippet.ts
Created June 12, 2025 18:12
Example of how useEffects still run when you switch away from a screen
const [count, setCount] = useState(0)
const [slowCount, setSlowCount] = useState(0)
const [isToggled, setIsToggled] = useState(false)
useEffect(() => {
console.log('slowCount useEffect ran')
const interval = setInterval(() => {
console.log('slow count incremented')
setSlowCount((prevState) => prevState + 1)