我如何将函数传递给导航(堆栈)组件

发布于 2025-01-10 04:25:16 字数 2293 浏览 0 评论 0原文

所以我想将savedLocationHandler()函数传递给导航组件,这样我就可以将它放在标题按钮上,因为我猜这是访问标题的唯一方法,但我不知道该怎么做!我尝试了 setParams 但它不起作用,我只知道路由方法,但我猜这只能从一个屏幕到另一个屏幕使用?!

import React, { useCallback, useEffect, useState } from "react";
import { Button, StyleSheet, Text, View } from "react-native";
import MapView, { Marker } from "react-native-maps";

const MapScreen = ({ navigation }) => {
  const [selectedLocation, setSelectedLocation] = useState();
  const MapRegion = {
    latitude: 37.78,
    longitude: -122.43,
    latitudeDelta: 0.0922,
    longitudeDelta: 0.0421,
  };
  const selectLocationHandler = (event) => {
    console.log(event);
    setSelectedLocation({
      lat: event.nativeEvent.coordinate.latitude,
      lng: event.nativeEvent.coordinate.longitude,
    });
  };
  let markerCoordinates;

  const savedLocationHandler = () => {
    if (!selectedLocation) {
      return;
    }
    navigation.navigate("NewPlaceScreen", {
      selectedLocation: selectedLocation,
    });
  };

  if (selectedLocation) {
    markerCoordinates = {
      latitude: selectedLocation.lat,
      longitude: selectedLocation.lng,
    };
  }

  return (
    <MapView
      style={styles.mapImage}
      region={MapRegion}
      onPress={selectLocationHandler}
    >
      {markerCoordinates && (
        <Marker title="Picked Location" coordinate={markerCoordinates}></Marker>
      )}
    </MapView>
  );
};  

这是导航组件(仅在我想传递函数的地方)

<Stack.Screen
          name="MapScreen"
          component={MapScreen}
          options={({ navigation }) => ({
            headerRight: () => (
              <TouchableOpacity
                onPress={() => { {/* i want to pass the function here*/ } 
                  saveFn;
                }}
                style={{ marginHorizontal: 20 }}
              >
                <Text
                  style={{
                    fontSize: 19,
                    fontWeight: "bold",
                    color: Platform.OS === "android" ? "#C06E00" : "white",
                  }}
                >
                  Save
                </Text>
              </TouchableOpacity>
            ),
          })}
        />
      </Stack.Navigator>

so I wanted to pass the savedLocationHandler() function to the navigation component so I can put it on the Header button because i guess that's the only way to access the header but I didn't know how to do that ! i tried setParams and it didn't work , I only know the route method but that's only used from a screen to another i guess ?!

import React, { useCallback, useEffect, useState } from "react";
import { Button, StyleSheet, Text, View } from "react-native";
import MapView, { Marker } from "react-native-maps";

const MapScreen = ({ navigation }) => {
  const [selectedLocation, setSelectedLocation] = useState();
  const MapRegion = {
    latitude: 37.78,
    longitude: -122.43,
    latitudeDelta: 0.0922,
    longitudeDelta: 0.0421,
  };
  const selectLocationHandler = (event) => {
    console.log(event);
    setSelectedLocation({
      lat: event.nativeEvent.coordinate.latitude,
      lng: event.nativeEvent.coordinate.longitude,
    });
  };
  let markerCoordinates;

  const savedLocationHandler = () => {
    if (!selectedLocation) {
      return;
    }
    navigation.navigate("NewPlaceScreen", {
      selectedLocation: selectedLocation,
    });
  };

  if (selectedLocation) {
    markerCoordinates = {
      latitude: selectedLocation.lat,
      longitude: selectedLocation.lng,
    };
  }

  return (
    <MapView
      style={styles.mapImage}
      region={MapRegion}
      onPress={selectLocationHandler}
    >
      {markerCoordinates && (
        <Marker title="Picked Location" coordinate={markerCoordinates}></Marker>
      )}
    </MapView>
  );
};  

and this is the navigation component ( only where i want to pass the function )

<Stack.Screen
          name="MapScreen"
          component={MapScreen}
          options={({ navigation }) => ({
            headerRight: () => (
              <TouchableOpacity
                onPress={() => { {/* i want to pass the function here*/ } 
                  saveFn;
                }}
                style={{ marginHorizontal: 20 }}
              >
                <Text
                  style={{
                    fontSize: 19,
                    fontWeight: "bold",
                    color: Platform.OS === "android" ? "#C06E00" : "white",
                  }}
                >
                  Save
                </Text>
              </TouchableOpacity>
            ),
          })}
        />
      </Stack.Navigator>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾城如夢未必闌珊 2025-01-17 04:25:16

我们可以使用 useLayoutEffect 为任何屏幕的 headerRight 属性提供一个函数,如下所示。

const MapScreen = ({ navigation }) => {

...


useLayoutEffect(() => {
    navigation.setOptions({
      headerRight: () => console.log("DO SOMETHING")
    })
}, [])

...

}

We could provide a function for the headerRight prop for any screen using the useLayoutEffect as follows.

const MapScreen = ({ navigation }) => {

...


useLayoutEffect(() => {
    navigation.setOptions({
      headerRight: () => console.log("DO SOMETHING")
    })
}, [])

...

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文