我想为执行此操作的函数创建一个文件 (function.js):
let i = 0;
if (this === that) {
i = 0;
} else {
i = 1;
}
然后我想将它添加到 (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>
最佳答案
你可以这样做:
函数.js
const doSomething = function() {
let i = 0;
if (this === that) {
i = 0;
} else {
i = 1;
}
}
export default doSomething;
App.js(例如):
import doSomething from "./function";
class Example extends Component {
state = {
test
};
render() {
doSomething()
return (
<div>
<h1>{this.state.test.sample[i].name}</h1>
</div>
)
关于javascript - 我如何在 react 中使用另一个文件中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54199264/