简体   繁体   English

导出默认 function 在 ReactJS 中返回未定义的值

[英]export default function returns undefined value in ReactJS

ok, so, I want to separate my security related functions in a separate functional component.好的,所以,我想将我的安全相关功能分离到一个单独的功能组件中。 For this I ended up creating the below functional component为此,我最终创建了以下功能组件

import { useAuthDispatch, logout, useAuthState } from "../context/context";

const AppSecurity = () => {
  

  const isAdminUser = () => {
    console.log("+++ isAdminUser called...");
    return true;
  };

  const isSuperUser = () => {
    console.log("+++ isSuperUser called...");
    return true;
  };
};

export default AppSecurity;

The grand plan is to implement both isAdminUser and isSuperUser functions eventually to go through security contexts and return true or false based on the logged in user but that's later some time.宏伟的计划是通过安全上下文最终实现 isAdminUser 和 isSuperUser 函数到 go 并根据登录用户返回真或假,但那是稍后的一段时间。 My current issue is when I import this AppSecurity functional component is other functional component and try to access the isAdminUser and isSuperUser functions, I get undefined values...see below我当前的问题是当我导入此 AppSecurity 功能组件是其他功能组件并尝试访问isAdminUserisSuperUser函数时,我得到未定义的值...见下文

import AppSecurity from '../../utils/security'

function Header() {

  const isAdminUser = AppSecurity.isAdminUser;
  const isSuperUser = AppSecurity.isSuperUser;

  console.log('isAdminUser : ', isAdminUser)
  console.log('isAdminUser : ', isSuperUser)
}

export default Header;

in the above code BOTH console log returns ' undefined ' values.在上面的代码中,控制台日志都返回“未定义”值。 I am also noticing that none of the console logging inside those 2 functions within the AppSecurity shows up.我还注意到,AppSecurity 中这两个函数内的控制台日志均未显示。 I tried to change the way I access those two functions in my Header as per below and getting webpack error that those are not functions....Any idea?我试图改变我在 Header 中访问这两个函数的方式,如下所示,并得到 webpack 错误,那些不是函数......知道吗?

const isAdminUser = AppSecurity.isAdminUser();
const isSuperUser = AppSecurity.isSuperUser();

see below webpack error...见下文 webpack 错误...

TypeError: _utils_security__WEBPACK_IMPORTED_MODULE_17__.default.isAdminUser is not a function
Header
src/components/ui/header.component.jsx:200
  
> 200 | const isAdminUser = AppSecurity.isAdminUser();
      | ^  201 | const isSuperUser = AppSecurity.isSuperUser();
  202 | console.log('isAdminUser : ', isAdminUser)
  203 | const toggleDrawer = () => {

You are not returning anything from the AppSecurity function.您不会从AppSecurity返回任何内容。 You need to add the following return statement in the AppSecurity function.您需要在AppSecurity中添加以下返回语句。

  return { isAdminUser, isSuperUser };

you need to export it like, since AppSecurity is a function.您需要像这样导出它,因为AppSecurity是 function。

export default AppSecurity();

You need to return an object that contains both isAdminUser and isSuperUser property in an object您需要在 object 中返回包含isAdminUserisSuperUser属性的 object

const AppSecurity = () => {
  

  const isAdminUser = () => {
    console.log("+++ isAdminUser called...");
    return true;
  };

  const isSuperUser = () => {
    console.log("+++ isSuperUser called...");
    return true;
  };

  return { isAdminUser, isSuperUser };
};

export default AppSecurity();

You are Exporting a function which in having local functions, try exporting an Object like below您正在导出具有本地功能的 function,尝试导出 Object,如下所示

 import { useAuthDispatch, logout, useAuthState } from "../context/context"; const AppSecurity = { isAdminUser: () => { console.log("+++ isAdminUser called..."); return true; }, isSuperUser: () => { console.log("+++ isSuperUser called..."); return true; } }; export default AppSecurity;

First of all thanks everyone who quickly posted various answers.首先感谢所有快速发布各种答案的人。 I must admit that I was missing a few basic stuff.我必须承认我遗漏了一些基本的东西。 After making below changes, now I can call those functions and can also use the useAuthState() hook inside those functions.进行以下更改后,现在我可以调用这些函数,也可以在这些函数中使用 useAuthState() 挂钩。 The issue turned out to be uppercase first letter of the function names..see below... IsAdminUser and IsSuperUser instead of is AdminUser and is SuperUser Once again basic mistake on my side.问题原来是 function 名称的大写首字母..见下文... IsAdminUser 和 IsSuperUser 而不是is AdminUser 和is SuperUser 又是我这边的基本错误。

import React from 'react';
import { useAuthDispatch, logout, useAuthState } from "../context";

  export const IsAdminUser = () => {
    const userDetails = useAuthState();
    console.log("+++ IsAdminUser called...");
    return true;
  };

  export const IsSuperUser = () => {
    console.log("+++ IsSuperUser called...");
    return false;
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM