[英]how to import function in reactjs
AppFunction.js AppFunction.js
export default function myFunction() {
var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);
document.getElementById("demo").innerHTML = res;
}
App.js App.js
import React, { Component } from 'react';
import myFunction from './AppFunction';
class App extends Component {
render() {
return (
<div className="App">
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</div>
);
}
}
export default App;
I should to import an existing JavaScript library and then call it's functions. 我应该导入一个现有的JavaScript库,然后调用它的函数。
componentDidMount() {
myFunction()
}
When I write like this, working. 当我这样写的时候,工作。 But shows on screen without clicking the onclick button.
但是无需单击onclick按钮即可在屏幕上显示。 I want to press the button and show it on the screen.
我想按下按钮并在屏幕上显示它。
Change 更改
<button onclick="myFunction()">Try it</button>
to 至
<button onClick={myFunction}>Try it</button>
You should use 你应该用
export default function myFunction() {
var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);
return res;
}
and 和
<button onClick={myFunction}>Try it</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.