英文:
React component not returning expected value when imported into another component
问题
以下是您要翻译的内容:
I am having trouble getting a boolean
value from a separate component file to display properly in my main App.js
file.
I created a separate file called Component.js
where I defined a boolean variable called "test" and exported it. In App.js
, I imported the variable and logged it to the console. However, the console is showing me the entire Component
function definition instead of the boolean
value.
This is my App.js
:
import React, { useState } from "react";
import "./App.css";
import test from './Component';
export default function App() {
console.log(test)
return (
<>
</>
)
}
This is my Component.js
import React, { useState } from "react";
export const test = true;
export default function Component() {
}
英文:
I am having trouble getting a boolean
value from a separate component file to display properly in my main App.js
file.
I created a separate file called Component.js
where I defined a boolean variable called "test" and exported it. In App.js
, I imported the variable and logged it to the console. However, the console is showing me the entire Component
function definition instead of the boolean
value.
This is my App.js
:
import React, { useState } from "react";
import "./App.css";
import test from './Component'
export default function App() {
console.log(test)
return (
<>
</>
)
}
This is my Component.js
import React, { useState } from "react";
export const test = true;
export default function Component() {
}
答案1
得分: 2
你已导入了默认导出
仅导入测试模块
import React, { useState } from "react";
import "./App.css";
import { test } from './Component';
export default function App() {
console.log(test);
return (
<>
</>
);
}
英文:
You have imported the default export
import only test module
import React, { useState } from "react";
import "./App.css";
import { test } from './Component'
export default function App() {
console.log(test)
return (
<>
</>
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论