getting value undefined into function, the function is declare into another js file and used inside

编程入门 行业动态 更新时间:2024-10-05 01:19:23

getting value undefined into function, the function is <a href=https://www.elefans.com/category/jswz/34/1502351.html style=declare into another js file and used inside the component in react"/>

getting value undefined into function, the function is declare into another js file and used inside the component in react

我有一个名为 test.js 的 js 文件,里面有一个函数

export const a =(data)=>{
                 console.log(data)
}

我有一个名为 File.js 的功能组件

import React, {useEffect} from "react";
import {a} from "./test";

let data = "hello"

function File() {
useEffect(()=> {a(data)},[]}
  return ( <div>something</div>)
}

export default File;

我在 console.log 中得到输出未定义,我应该得到“你好”。我不明白是什么问题请帮助我

回答如下:

修复

useEffect()
错误的括号类型问题后,代码应该可以正常工作:

文件.js

import React, { useEffect } from 'react';
import { a } from './test';

let data = 'hello';

function File() {
  useEffect(() => a(data), []);
  return <div>something</div>;
}

export default File;

这里我在一个最小的代码环境中实现了它,它正在按预期打印

hello

环境链接

StackBlitz 虚拟环境链接

更多推荐

getting value undefined into function, the function is declare into another js f

本文发布于:2024-05-30 20:32:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:declare   function   undefined   js   react

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!