Skip to content

Commit b1c33f9

Browse files
committed
Supported refetching the whole scene
Pass true to the refetch and it's the whole scene, otherwise it's just the scene view const refetch = useRefetch(); refetch(true) Tried setState(prevContext => nextContext) but this infinitely looped. Don't think it's supported in startTransition
1 parent 0fdb8bb commit b1c33f9

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

NavigationReact/src/NavigationHandler.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use client'
2-
import React, { Component } from 'react';
2+
import React, { Component, startTransition } from 'react';
33
import { StateNavigator, State } from 'navigation';
44
import AsyncStateNavigator from './AsyncStateNavigator.js';
55
import NavigationContext from './NavigationContext.js';
6+
import SceneRSCContext from './SceneRSCContext.js';
67
type NavigationHandlerState = { context: { ignoreCache?: boolean, oldState: State, state: State, data: any, asyncData: any, stateNavigator: AsyncStateNavigator } };
78

89
class NavigationHandler extends Component<{ stateNavigator: StateNavigator, children: any }, NavigationHandlerState> {
@@ -13,6 +14,7 @@ class NavigationHandler extends Component<{ stateNavigator: StateNavigator, chil
1314
const asyncNavigator = new AsyncStateNavigator(this, stateNavigator, stateNavigator.stateContext);
1415
this.state = { context: { oldState, state, data, asyncData, stateNavigator: asyncNavigator } };
1516
this.onNavigate = this.onNavigate.bind(this);
17+
this.refetch = this.refetch.bind(this);
1618
}
1719

1820
componentDidMount() {
@@ -33,10 +35,18 @@ class NavigationHandler extends Component<{ stateNavigator: StateNavigator, chil
3335
}
3436
}
3537

38+
refetch() {
39+
startTransition(() => {
40+
this.setState({ context: { ...this.state.context, ignoreCache: true } });
41+
});
42+
}
43+
3644
render() {
3745
return (
3846
<NavigationContext.Provider value={this.state.context}>
39-
{this.props.children}
47+
<SceneRSCContext.Provider value={this.refetch}>
48+
{this.props.children}
49+
</SceneRSCContext.Provider>
4050
</NavigationContext.Provider>
4151
);
4252
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createContext } from 'react';
2+
3+
export default createContext<() => void>(null);

NavigationReact/src/useRefetch.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { useContext } from 'react';
22
import { StateContext } from 'navigation';
33
import RSCContext from './RSCContext.js';
4+
import SceneRSCContext from './SceneRSCContext.js';
45

56
const useRefetch = (refetch?: string[] | ((stateContext: StateContext) => boolean) | null) => {
67
const {setRefetch, refetcher} = useContext(RSCContext);
8+
const sceneRefetcher = useContext(SceneRSCContext);
79
setRefetch(refetch);
8-
return refetcher;
10+
return (scene = false) => !scene ? refetcher() : sceneRefetcher();
911
}
1012
export default useRefetch;

0 commit comments

Comments
 (0)