Problem:- I have written a function named "getData()" in "App.js" and i am able to call it in "User.js". Code is written below.
But, What if i have written that "getdata()" function in "User.js" and i have to call it in "App.js". How can i do that? i am not able to do so.. please guide me.
App.js
import User from './User'
function App() {
function getData() {
alert("Hello from app component")
}
return (
<div className="App">
<User data={getData}/>
</div>
);
}
export default App:
User.js
function User(props) {
return(
<div>
<h1>User Component</h1>
<button onClick={props.data}> Call Function</button>
</div>
export default User;
getDatafrom your UI code. Put the function in a seperate file and import it