英文:
getting value undefined into function, the function is declare into another js file and used inside the component in react
问题
I have one JS file called test.js and I have one function inside it:
export const a = (data) => {
console.log(data);
}
And I have a functional component called 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;
I get the output in console.log
as undefined, but I should get "hello." I don't understand what the problem is. Please help me.
英文:
i have one js file called test.js and i have one function inside it
export const a =(data)=>{
console.log(data)
}
and i have a functional component called 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;
and i get output in console.log is undefined, i should get "hello". i dont understand what the problem please help me
答案1
得分: 1
After fixing the useEffect()
wrong bracket type issue the code should work just fine:
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;
Here I've implemented it in a minimal code environment, and it is printing hello
as intended.
Environment Link
StackBlitz Virtual Environment Link
英文:
After fixing the useEffect()
wrong bracket type issue the code should work just fine:
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;
Here I've implemented it in a minimal code environment, and it is printing hello
as intended.
Environment Link
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论