Skip to content

Instantly share code, notes, and snippets.

@shirohana
Last active February 15, 2020 09:03
Show Gist options
  • Save shirohana/b648bb1f8bea9294302fd0d085bb0750 to your computer and use it in GitHub Desktop.
Save shirohana/b648bb1f8bea9294302fd0d085bb0750 to your computer and use it in GitHub Desktop.
React Navigation Lock Example
// @flow
import { useCallback, useState } from 'react'
import { useFocusEffect } from '@react-navigation/native'
export default function useNavigateLock () {
const [isLocked, setIsLocked] = useState(false)
useFocusEffect(useCallback(() => {
setIsLocked(false)
}, []))
const locker = () => {
if (isLocked) {
return false
} else {
setIsLocked(true)
return true
}
}
return locker
}
import React from 'react'
import { Button, View } from 'react-native'
import useNavigateLock from '~hooks/use-navigate-lock'
const Home = ({ navigation }) => {
const lock = useNavigateLock()
const goSettings = () => lock() && navigation.push('Settings')
const goProfile = () => {
if (lock()) {
const params = { name: 'Hana' }
navigation.push('Profile', params)
}
}
return (
<View>
<Button title="pushSettings" onPress={goSettings} />
<Button title="pushProfile" onPress={goProfile} />
</View>
)
}
export default Home
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment