0

没有导航道具的导航之后,我无法在打字稿中使引用可变以分配current. 面临错误

无法分配给“当前”,因为它是只读属性。

"react": "17.0.1",
"react-native": "0.64.0",
"@types/react-native": "^0.64.0",

index.js我有

PushNotification.configure({
  onNotification: (notification) => {
    navigateToScreen("screenName",{params})
    notification.finish(PushNotificationIOS.FetchResult.NoData);
  },
  /* othe config attributes */
});

navigation.tsx我有

    <NavigationContainer ref={navigationContainerRef} onReady={() => (navigationReady.current = true)}>
      /* drawer stack ...*/
    </NavigationContainer>

引用从哪里导出值ntfService.ts

export const navigationContainerRef = React.createRef<NavigationContainerRef | null>();
export let navigationReady = React.createRef<boolean>();

export const navigateToScreen = (scrName:string,params:{id:number}) => {
  if (navigationContainerRef.current && navigationReady.current) {
    navigationContainerRef.current.navigate(scrName, params);
  } else {
    /** */
  }
};

按照GitHub 问题中的答案,使用 |null 定义 ref 类型应该使其可变。但似乎就useRef这样。

4

0 回答 0