首页 > 解决方案 > 如何在 React Native 中从功能组件调用函数?

问题描述

从 Class 组件调用函数很容易。但是如何从另一个类或功能组件中的功能组件调用函数呢?

任何解决方案。我在stackOverflow上搜索了很多教程,但仍然没有解决方案。请提供任何解决方案。

抽屉设置.dart

        import * as React from 'react';
    import { Button, View, Text, TouchableOpacity } from 'react-native';
    import {
      createDrawerNavigator, DrawerContentScrollView,
      DrawerItemList,
      DrawerItem,
    } from '@react-navigation/drawer';
    import { NavigationContainer } from '@react-navigation/native';
    import Resources from '../DrawerScreens/Resources';
    import Themes from '../DrawerScreens/Themes';
    import AboutUs from '../DrawerScreens/AboutUs';
    import CustomSidebarMenu from './CustomSidebarMenu';
    import Login from '../Validation/Login';
    import AsyncStorage from '@react-native-async-storage/async-storage';


    const Drawer = createDrawerNavigator();

    export default function DrawerSetting({route, navigation }) {

    console.log("email is = " + password);

      add = () =>{
        console.log("Addition called");
      }


      AsyncStorage.getItem("userId").then((val) => {
        console.log("Drawer valuse are=" + val);
      });
        

      return (
        <NavigationContainer independent={true} >
          <Drawer.Navigator initialRouteName="Resources" drawerContent={(props) => <CustomSidebarMenu {...props} />}

          >
            <Drawer.Screen name="Resources" component={Resources}
              options={{ drawerLabelStyle: { fontSize: 16, color: 'black', fontWeight: 'bold', } }}
            />
            <Drawer.Screen name="Themes" component={Themes}
              options={{ drawerLabelStyle: { fontSize: 16, color: 'black', fontWeight: 'bold', } }}
            />
            <Drawer.Screen name="About Us" component={AboutUs}
              options={{ drawerLabelStyle: { fontSize: 16, color: 'black', fontWeight: 'bold', } }}
            />


          </Drawer.Navigator>
        </NavigationContainer>
      );
    }

如何从另一个类或功能组件中的 DrawerSetting() 组件调用 -> add() 函数?

标签: react-nativereact-hooksreact-routerreact-navigation

解决方案


推荐阅读