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
发布评论