React Sample Programs
React Sample Programs
</div>
);
}
}
class App extends React.Component {
render() {
return (
<div>
<First/>
<Second/>
</div>
);
}
}
export default App;
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Sudha</td>
<td>21</td>
</tr>
<tr>
<td>Nikhil</td>
<td>32</td>
</tr>
</table>
</>
);
}
}
export default App;
Example-
2. /*****---Class Component with files********/
Program to create a multiple child components in different files
App.js
import './App.css';
import React,{Component} from 'react';
import Welcome from './components/Welcome.js';
import Dept from './components/Dept.js';
import Tabl from './components/Tabl.js';
Welcome.js
Dept.js
function Dept() {
return <h2>Hi, I am from MCA!</h2>;
}
export default Dept;
Tabl.js
class Tabl extends React.Component{
render(){
return (
<table>
<tr style={{"borderWidth":"1px", 'borderColor':"#aaaaaa",
'borderStyle':'solid'}}>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Sudha</td>
<td>21</td>
</tr>
<tr>
<td>Nikhil</td>
<td>32</td>
</tr>
</table>
);
}
}
export default Tabl;
Branch.js
const Branch = (props) => {
return < h1 > {props.name} </ h1>
}
export default Branch;
<ol>
<li> Subject code: {this.props.code} </li>
<li> Subject Name: {this.props.name}</li>
<li> Semester : {this.props.sem} </li>
</ol> </ul>
</>
)
}
}
OUTPUT:
this.state = {
dep: "MCA",
sub: "Data Science"
};
}
changeIDE = () => {
this.setState({sub: "Full Stack"});
}
render() {
return (
<div>
<h2>Department is {this.state.dep}</h2>
<p style={{color: "red"}}>
My IDE is {this.state.sub}
</p>
<button type="button" onClick={this.changeIDE}>Change IDE</button>
</div>
);
}
}
export default Subject;
OUTPUT:
OUTPUT:
<br />
<button onClick={()=>setShow(!show)}> Toggle</button>
</div>
)
}
export default App;
Notice that we are setting the show as ‘not show’. Initially, the show is true. On clicking the button, it
will become false. On clicking again, it will become true. This will go on and the value of the show will
be inverted with each click.
OUTPUT:
function App() {
const[data,setData]=useState(null)
function getData(val)
{
setData(val.target.value)
console.warn(val.target.value)
}
return(
<div className="App">
<h1>{data}</h1>
<input type="text" onChange={getData} />
</div>
);
}
export default App;
OUTPUT:
App.js
import './App.css';
import Login from './eventhandle/Login.js';
function App() {
return (
<div className="App">
<Login />
</div>
);
}
export default App;
Login.js
const[user,setUser]=useState("")
const[userErr,setUserErr]=useState(false)
function userhandle(e){
let item=e.target.value;
if(item.length<4){
setUserErr(true)
}
else{
setUserErr(false)
}
setUser(item)
}
function formhandle(e){
if(user.length<4){
alert("Invalid Username")
}
else{
alert("Valid User name")
}
}
return (
<div className="App">
<h1>Login Form</h1>
<form onSubmit={formhandle}>
User Name :<input type="text" placeholder="Enter user name"
onChange={userhandle} /> <br />
<button type="submit">LOGIN</button>
</form>
</div>
);
} export default Login;
OUTPUT:
------------------------------------------------------------------------------------------------------------------------------------
A controlled component takes its current value through props and notifies the changes
through callbacks like an onChange event. A parent component "controls" this changes by
handling the callback and managing its own state and then passing the new values as props to
the controlled component.
App.js
import './App.css';
import Boxvalue from './eventhandle/Boxvalue';
function App() {
return (
<div className="App">
<Boxvalue />
</div>
);
}
export default App;
Boxvalue.js
import React from 'react';
class Boxvalue extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.formSubmit = this.formSubmit.bind(this);
}
handleChange(e) {
this.setState({value: e.target.value});
}
formSubmit(e) {
alert('You have submitted the input successfully: ' + this.state.value);
//event.preventDefault();
}
render(){
return (
<div className="App">
<header className="App-header">
<h3>Textbox element values</h3>
<form onSubmit={this.formSubmit}>
OUTPUT: