Replies: 4 comments 1 reply
-
Beta Was this translation helpful? Give feedback.
-
|
so for every edit i have to navigate back to the page i was editing manually? |
Beta Was this translation helpful? Give feedback.
-
|
I know this is not recommended 'n all, but I find this helpful for development. You can use the storage provider of your choice (I'm using mmkv) and probably fix the types a bit (i'm too lazy), but this works for me so far. Notes:
I'll keep updating this example as i run into issues or need more functionality. It's a start though. import { Consts } from '@myapp/shared'
import { storage } from '@myapp/shared/src/utils/storage'
import type { NavigationState } from '@react-navigation/native'
import { useNavigationContainerRef, useRootNavigationState } from 'expo-router'
import { useEffect, useState } from 'react'
interface RootStateOrRoute {
index?: number
routes?: RootStateOrRoute[]
name?: string
state?: any
[key: string]: any
}
function stripState(payload: RootStateOrRoute) {
if (typeof payload !== 'object' || payload === null) {
return undefined
}
const { index, routes, name, state } = payload
const strippedRoutes = routes
? routes.map((route: any) => stripState(route)).filter((route: RootStateOrRoute | undefined) => route !== undefined)
: undefined
const strippedState = state ? stripState(state) : undefined
const strippedPayload: RootStateOrRoute = { index, routes: strippedRoutes, name, state: strippedState }
return Object.keys(strippedPayload).length > 0 ? strippedPayload : undefined
}
export function useNavigationStatePersistence() {
if (Consts.isDev) {
const [isNavigationRehydrated, setIsNavigationRehydrated] = useState(false)
const navRef = useNavigationContainerRef()
const state = useRootNavigationState()
useEffect(() => {
if (!state) return
if (isNavigationRehydrated) {
storage.save('navigationState', state)
}
if (navRef.current?.isReady() && !isNavigationRehydrated) {
const prevState = storage.load<RootStateOrRoute>('navigationState')
if (!!prevState) {
navRef.current?.resetRoot(stripState(prevState!) as NavigationState)
}
setIsNavigationRehydrated(true)
}
}, [state, isNavigationRehydrated])
return isNavigationRehydrated
}
return true
} |
Beta Was this translation helpful? Give feedback.
-
|
I'm looking to implement the same functionality, since I'm migrating a project from react-navigation to expo-router, anyone got any updates on this? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like to achieve the same functionality as defined here with expo router:
https://reactnavigation.org/docs/state-persistence
Is there a straight forward way to implement it?
Beta Was this translation helpful? Give feedback.
All reactions