Pass Function as Props - React Js
App.js:
import React, { useState } from 'react';
import './App.css'
import Student from './Components/Student';
function App()
{
function callChildComponent()
{
alert("Called Student Component from App.js")
}
return(
<div className='App'>
<Student data={callChildComponent}/>
</div>
)
}
export default App;
-----------------------
Student.js:
import React from "react";
function Student(props)
{
return(
<div>
<h1>Student Component</h1>
<button onClick={props.data}>Call App.js Function</button>
</div>
)
}
export default Student;
Comments
Post a Comment