如何在React中使用另一个文件中的函数? [英] How can I use a function from another file in react?
本文介绍了如何在React中使用另一个文件中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为执行此操作的函数创建文件(function.js):
I would like to create a file (function.js) for a function that does this:
let i = 0;
if (this === that) {
i = 0;
} else {
i = 1;
}
然后我想将其添加到(this.js)
I would then like to add it to (this.js)
import function from "./function";
class Example extends Component {
state = {
test
};
render() {
function()
return (
<div>
<h1>{this.state.test.sample[i].name}</h1>
</div>
推荐答案
您可以执行以下操作:
function.js
function.js
const doSomething = function() {
let i = 0;
if (this === that) {
i = 0;
} else {
i = 1;
}
}
export default doSomething;
App.js(例如):
App.js (for example):
import doSomething from "./function";
class Example extends Component {
state = {
test
};
render() {
doSomething()
return (
<div>
<h1>{this.state.test.sample[i].name}</h1>
</div>
)
这篇关于如何在React中使用另一个文件中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文