react-navigation icon indicating copy to clipboard operation
react-navigation copied to clipboard

`navigate` method type error

Open thormengkheang opened this issue 4 years ago • 5 comments

Current behavior

I got type error while I write my custom navigateTo function.

function HomeScreen() {
  const navigation = useNavigation<StackNavigationProp<RootStackParamList>>();

  function navigateTo(
    screen: keyof RootStackParamList,
    params?: RootStackParamList[keyof RootStackParamList],
  ) {
    navigation.navigate(screen, params);
  }

  return (
    <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
      <Text>Home Screen</Text>
    </View>
  );
}
image
import React from 'react';
import {Text, View} from 'react-native';
import {
  createStackNavigator,
  StackNavigationProp,
} from '@react-navigation/stack';
import {
  NavigationContainer,
  RouteProp,
  useNavigation,
  useRoute,
} from '@react-navigation/native';

function HomeScreen() {
  const navigation = useNavigation<StackNavigationProp<RootStackParamList>>();

  function navigateTo(
    screen: keyof RootStackParamList,
    params?: RootStackParamList[keyof RootStackParamList],
  ) {
    navigation.navigate(screen, params);
  }

  return (
    <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
      <Text>Home Screen</Text>
    </View>
  );
}

function DetailsScreen() {
  const route = useRoute<RouteProp<RootStackParamList, 'Details'>>();
  return (
    <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
      <Text>Details Screen {route?.params?.userId}</Text>
    </View>
  );
}

type RootStackParamList = {
  Home: undefined;
  Details: {userId: string};
};

const Stack = createStackNavigator<RootStackParamList>();

const App = () => {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Details" component={DetailsScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

export default App;

Expected behavior

Should not have type error because with v5 the type is correct.

Reproduction

https://github.com/thormengkheang/TestRN

Platform

  • [X] Android
  • [X] iOS
  • [ ] Web
  • [ ] Windows
  • [ ] MacOS

Packages

  • [ ] @react-navigation/​bottom-tabs
  • [ ] @react-navigation/​drawer
  • [ ] @react-navigation/​material-bottom-tabs
  • [ ] @react-navigation/​material-top-tabs
  • [X] @react-navigation/​stack
  • [ ] @react-navigation/​native-stack
  • [ ] react-native-tab-view
  • [ ] flipper-plugin-react-navigation

Environment

  • [] I've removed the packages that I don't use
package version
@react-navigation/native ^6.0.14
@react-navigation/stack ^6.3.5
react-native-safe-area-context ^3.4.1
react-native-screens ^3.1.0
react-native 0.69.5
yarn 1.22.19

thormengkheang avatar Nov 28 '22 09:11 thormengkheang

Couldn't find version numbers for the following packages in the issue:

  • react-native-tab-view
  • flipper-plugin-react-navigation

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

github-actions[bot] avatar Nov 28 '22 09:11 github-actions[bot]

Also have the same issue. When I provide a screenName through a function param, I get the same behaviour

AlekseyP18 avatar Nov 30 '22 10:11 AlekseyP18

@thormengkheang @AlekseyP18 did you guys get any solution?

HER0PLAY avatar Jun 13 '24 20:06 HER0PLAY

https://github.com/react-navigation/react-navigation/discussions/11982#discussioncomment-9371942

satya164 avatar Jun 13 '24 20:06 satya164

Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro.

github-actions[bot] avatar Jun 13 '24 20:06 github-actions[bot]