【发布时间】:2021-04-06 10:33:54
【问题描述】:
我在我的 React Native 应用程序中使用 React Navigation 作为路由器。我有一个问题,即当我在屏幕之间导航时,之前在我们之前渲染的屏幕时创建的钩子仍然存在。
我有一个完全扁平的路由器:
const AppRouter = () => {
const Stack = createStackNavigator();
return (
<NavigationContainer ref={navigationRef}>
<Stack.Navigator headerMode={false}>
<Stack.Screen
component={Landing}
name="Landing"
options={fadeAnimation}
/>
<Stack.Screen
component={LogInOrSignUp}
name="LogInOrSignUp"
options={fadeAnimation}
/>
<Stack.Screen
component={Dashboard}
name="Dashboard"
options={horizontalAnimation}
/>
<Stack.Screen
component={Loader}
name="Loader"
options={horizontalAnimation}
/>
<Stack.Screen
component={WakingFromKilled}
name="Connecting"
/>
<Stack.Screen
component={EnterPhoneNumber}
name="EnterPhoneNumber"
options={horizontalAnimation}
/>
<Stack.Screen
component={EnterSmsCode}
name="EnterSmsCode"
options={horizontalAnimation}
/>
<Stack.Screen
component={CompleteProfile}
name="CompleteProfile"
options={horizontalAnimation}
/>
<Stack.Screen
component={Contacts}
name="Contacts"
options={horizontalAnimation}
/>
<Stack.Screen
component={TimeInput}
name="TimeInput"
options={horizontalAnimation}
/>
<Stack.Screen
component={InCallContainer}
name="InCallContainer"
options={horizontalAnimation}
/>
<Stack.Screen
component={ConnectingController}
name="ConnectingController"
options={horizontalAnimation}
/>
<Stack.Screen
component={AccountModal}
name="AccountModal"
/>
<Stack.Screen
component={NoInternetConnection}
name="NoInternetConnection"
options={horizontalAnimation}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
当我从Dashboard > TimeInput > InCallContainer > Dashboard > TimeInput > InCallContainer 导航时,第一次访问时在InCallContainer 中创建的钩子在第二次访问时仍然存在。
我正在使用以下方法导航:
import { StackActions } from '@react-navigation/native';
import * as React from 'react';
export const navigationRef = React.createRef();
export function navigate(...args) {
navigationRef.current.navigate(...args);
}
然后:
RootNavigation.navigate('InCallContainer');
我可以看出钩子被创建了多次,因为如果我在使用这些钩子的组件中登录,每次我导航到它们时,我都会得到一个更多的日志。所以当我第二次访问屏幕时有两个日志,依此类推。
我希望能够第二次导航到仪表板,当我这样做时,从内存中卸载/删除此导航之前的所有组件。
我尝试过替换、重置、推送、popToTop,但都没有达到预期的效果。
我怎样才能做到这一点?
TIA
更新:
我需要从 Redux Thunk 内部以编程方式导航到 InCallContainer:
export const answerCall = (calleeName) => (dispatch, getState) => {
const { session, callType } = getState().callData;
acceptCall(session, { calleeNameFromOwnContacts: calleeName });
dispatch(callData({ callState: INCOMING }));
stopRingtone();
RootNavigation.navigate('InCallContainer');
InCallManager.start({ media: callType === VIDEO ? 'video' : 'audio' });
InCallManager.turnScreenOn();
};
【问题讨论】:
标签: react-native react-navigation