我有反应:
import React from "react";
import { render } from "react-dom";
import Applic from "./ColorAdder/Applic";
const styles = {
fontFamily: "sans-serif",
textAlign: "center"
};
const App = () => <div style={styles}>Here I want to use my function and show results</div>;
render(<App />, document.getElementById("root"));
我可以创建另一个文件,只包含函数,而不是组件,例如getCurrentTime()
并在App.js中使用它?
如何导入它?
答案 0 :(得分:4)
是的,你可以这样做。 例如:
getCurrentTime.js
:
export default () => {
// your code...
}
App.js
:
import getCurrentTime from './geTCurrentTime';
const time = getCurrentTime();
console.log(time);
在您的示例中App.js
:
import React from "react";
import { render } from "react-dom";
import Applic from "./ColorAdder/Applic";
import getCurrentTime from './geTCurrentTime';
const styles = {
fontFamily: "sans-serif",
textAlign: "center"
};
const App = () => <div style={styles}>{getCurrentTime()}</div>;
render(<App />, document.getElementById("root"));
答案 1 :(得分:1)
是的,你可以这样,
**导出默认的somethingFunction = {}并在另一个文件中:
从'/.path_to_your_file'**
导入{somethingFunction}