Skip to content

Commit 77c7315

Browse files
committed
refactor: get rid of dangerously setState
1 parent cb89deb commit 77c7315

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/NavigationContainer.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const MISSING_CONTEXT_ERROR =
1818
export const NavigationStateContext = React.createContext<{
1919
state?: NavigationState | PartialState;
2020
getState: () => NavigationState | PartialState | undefined;
21-
setState: (state: NavigationState | undefined, dangerously?: boolean) => void;
21+
setState: (state: NavigationState | undefined) => void;
2222
key?: string;
2323
performTransaction: (action: () => void) => void;
2424
}>({
@@ -67,17 +67,12 @@ export default class NavigationContainer extends React.Component<Props, State> {
6767
this.navigationState || this.state.navigationState;
6868

6969
private setNavigationState = (
70-
navigationState: NavigationState | undefined,
71-
dangerously = false
70+
navigationState: NavigationState | undefined
7271
) => {
73-
if (this.navigationState === null && !dangerously) {
72+
if (this.navigationState === null) {
7473
throw new Error('setState need to be wrapped in a performTransaction');
7574
}
76-
if (dangerously) {
77-
this.setState({ navigationState });
78-
} else {
79-
this.navigationState = navigationState;
80-
}
75+
this.navigationState = navigationState;
8176
};
8277

8378
render() {

src/useNavigationBuilder.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ export default function useNavigationBuilder(
9494
// If the state needs to be updated, we'll schedule an update with React
9595
// setState in render seems hacky, but that's how React docs implement getDerivedPropsFromState
9696
// https://reactjs.org/docs/hooks-faq.html#how-do-i-implement-getderivedstatefromprops
97-
setState(nextState, true);
97+
performTransaction(() => {
98+
setState(nextState);
99+
});
98100
}
99101

100102
state = nextState;

0 commit comments

Comments
 (0)