Правильно передать параметр функции в React Native - PullRequest
0 голосов
/ 03 марта 2020

Я изо всех сил пытаюсь передать const navigation = useNavigation(); должным образом своей внешней функции.

В настоящее время у меня есть эта 'главная' функция:

export default function MyDrawer({ route }) {
  const nav = useNavigation(); // Use hook here as u will now dont get any error. now pass it as param to above if needed

  return (
    <NavigationContainer independent={true}>
      <Drawer.Navigator
        initialRouteName="QR"
        drawerContent={props => CustomDrawerContent(props, route)}
      >
        <Drawer.Screen
          name="QR"
          component={QR}
          initialParams={{ user: route.params.user }}
        />
        <Drawer.Screen name="Odabir" component={Odabir} />
      </Drawer.Navigator>
    </NavigationContainer>
  );
}

, которая обрабатывает логику ящика c и маршрутизация. Раньше я объявлял const Drawer = createDrawerNavigator() в этой функции:

function CustomDrawerContent({ props, navigation }, route, ) {
  // *** Don't use below hook here its wrong to use hooks outside main function if need pass to it as param from main function
  // const navigation = useNavigation();
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View
        style={{
          height: 150,
          alignItems: "center",
          justifyContent: "center",
          marginTop: Platform.OS === "ios" ? 0 : StatusBar.currentHeight
        }}
      >
        <Image
          source={{uri:route.params.user.photoUrl}}
          style={{ height: 110, width: 110, borderRadius: 60 }}
        />
      </View>
      <View style={{ height: 30, alignItems: "center" }}>
        <Text style={styles.naslov}>
          {route.params.user.name} {route.params.user.lastname}
        </Text>
      </View>
      <View style={{ height: 30, alignItems: "center" }}>
        <Text style={styles.naslov}>
          {route.params.user.email} 
        </Text>
      </View>
      <View style={{ height: 30, alignItems: "center", marginBottom: 10 }}>
        <Text style={styles.naslov}></Text>
      </View>
      <Divider />
      <ScrollView style={{ marginLeft: 5 }}>
        <TouchableOpacity
          style={{ marginTop: 20, marginLeft: 20 }}
          onPress={() => props.navigation.navigate("QR")}
        >
          <View style={{ padding: 10 }}>
            <Ionicons name="ios-qr-scanner" size={20} styles={{}}>
              <Text style={styles.naslov}> QR</Text>
            </Ionicons>
          </View>
        </TouchableOpacity>
        <TouchableOpacity
          style={{ marginTop: 20, marginLeft: 20 }}
          onPress={() => props.navigation.navigate("Odabir")}
        >
          <View style={{ padding: 10 }}>
            <Ionicons name="ios-qr-scanner" size={20} styles={{}}>
              <Text style={styles.naslov}> Odabir</Text>
            </Ionicons>
          </View>
        </TouchableOpacity>
        <TouchableOpacity
          style={{ marginTop: 20, marginLeft: 20 }}
          onPress={() => props.navigation.navigate("Odabir")}
        >
          <View style={{ padding: 10 }}>
            <Ionicons name="ios-qr-scanner" size={20} styles={{}}>
              <Text style={styles.naslov}> Odabir</Text>
            </Ionicons>
          </View>
        </TouchableOpacity>
      </ScrollView>
    </SafeAreaView>
  );
}

, которая обрабатывает маршрутизацию onPress для различных компонентов. Мне пришлось переместить const nav = useNavigation(); из-за того, как работают хуки и процесс сбора данных из Google API. У меня вопрос, как правильно передать параметр обратно функции MyDrawer, чтобы навигация снова заработала? В настоящее время я получаю ошибку TypeError: undefined is not an object(evaluating 'props.navigation')

1 Ответ

0 голосов
/ 03 марта 2020

код обновления:

export default function MyDrawer({ route }) {
  const nav = useNavigation(); // Use hook here as u will now dont get any error. now pass it as param to above if needed

  return (
    <NavigationContainer independent={true}>
      <Drawer.Navigator
        initialRouteName="QR"
        drawerContent={props => <CustomDrawerContent {...props} {...{route}} />}
      >
        <Drawer.Screen
          name="QR"
          component={QR}
          initialParams={{ user: route.params.user }}
        />
        <Drawer.Screen name="Odabir" component={Odabir} />
      </Drawer.Navigator>
    </NavigationContainer>
  );
}

Содержимое ящика

const CustomDrawerContent = ({navigation, route}) => {
  // *** Don't use below hook here its wrong to use hooks outside main function if need pass to it as param from main function
  // const navigation = useNavigation();
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View
        style={{
          height: 150,
          alignItems: "center",
          justifyContent: "center",
          marginTop: Platform.OS === "ios" ? 0 : StatusBar.currentHeight
        }}
      >
        <Image
          source={{uri:route.params.user.photoUrl}}
          style={{ height: 110, width: 110, borderRadius: 60 }}
        />
      </View>
      <View style={{ height: 30, alignItems: "center" }}>
        <Text style={styles.naslov}>
          {route.params.user.name} {route.params.user.lastname}
        </Text>
      </View>
      <View style={{ height: 30, alignItems: "center" }}>
        <Text style={styles.naslov}>
          {route.params.user.email} 
        </Text>
      </View>
      <View style={{ height: 30, alignItems: "center", marginBottom: 10 }}>
        <Text style={styles.naslov}></Text>
      </View>
      <Divider />
      <ScrollView style={{ marginLeft: 5 }}>
        <TouchableOpacity
          style={{ marginTop: 20, marginLeft: 20 }}
          onPress={() => props.navigation.navigate("QR")}
        >
          <View style={{ padding: 10 }}>
            <Ionicons name="ios-qr-scanner" size={20} styles={{}}>
              <Text style={styles.naslov}> QR</Text>
            </Ionicons>
          </View>
        </TouchableOpacity>
        <TouchableOpacity
          style={{ marginTop: 20, marginLeft: 20 }}
          onPress={() => props.navigation.navigate("Odabir")}
        >
          <View style={{ padding: 10 }}>
            <Ionicons name="ios-qr-scanner" size={20} styles={{}}>
              <Text style={styles.naslov}> Odabir</Text>
            </Ionicons>
          </View>
        </TouchableOpacity>
        <TouchableOpacity
          style={{ marginTop: 20, marginLeft: 20 }}
          onPress={() => props.navigation.navigate("Odabir")}
        >
          <View style={{ padding: 10 }}>
            <Ionicons name="ios-qr-scanner" size={20} styles={{}}>
              <Text style={styles.naslov}> Odabir</Text>
            </Ionicons>
          </View>
        </TouchableOpacity>
      </ScrollView>
    </SafeAreaView>
  );
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...