Components In REACT
Components In REACT
Building Block Of UI
Components Introduction
Class Components
A class component must include the extends React.
Component statement. This statement creates an
inheritance to React. Component, and gives your component
access to React. Component's functions. The component also
requires a render() method, this method returns HTML
Components Introduction
Creating A Class Component
import react from ‘react’
class Calculator extends React.Component {
render() {
return <h2>Hi, I Will Calculate
Everything!</h2>;
}
}
Creating A Function Component
function Calculator() {
return <h2>Hi, I Will Calculate
Everything!</h2>;
}
Components Introduction
Rendering A Component
Now your React application has a component called
Calculator, which returns an <h2> element. To use this
component in your application, use similar syntax as normal
HTML.
<Calculator />
To display Calculator component in the root element
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Calculator />);
OR
const element=<Calculator/>
root.render(element)
Components Introduction
Separating The Concern
React is all about re-using code, and it is recommended to
split your components into separate files.To do that, create a
new file with a .js file extension and put the code inside it.
Create a file “Calculator.js”
function Calculator() {
return <h2>Hi, I Will Calculate
Everything!</h2>;
}
export default Car;
Recat Event
<button onClick={hitsix}>Take the Shot!</button>
HTML Event
<button onclick=“hitsix()">Take the Shot!</button>
React Components Event
Adding event inside a react component is as simple as passing a
function to the child component as a prop. Here we are adding
hitsix event to cricket component
function Cricket() {
const hitsix = () => {
alert(“Scorer Add Six Run To Score!!!");
}
return (
<button onClick={hitsix}>Take the shot!</button>
);
}
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Cricket />);
React Components Event
To pass an argument to an event handler, use an arrow operator.
Send “6 Runs” as a parameter to the hitsix function using arrow
function
function Cricket() {
const hitsix = (value) => {
console.log(`Add ${value} To Score`)
}
return (
<button onClick={()=>hitsix(“6 Runs”)}>Take the
shot!</button>
);
}
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Cricket />);
React Components Event
React Event Object
Event handlers have access to the React event that triggered the
function.
In our example the event is the "click" event
function Cricket() {
const hitsix = (value,event) => {
console.log(`Add ${value} To Score On ${event.type}`)
}
return (
<button onClick={(event)=>hitsix(“6 Runs”,event)}>Hit
Shot</button>
);
}
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Cricket />);
Conditional Rendering
In React, you can conditionally render components. There are several
ways to do this.
if statement
We can use the if JavaScript operator to decide which component to
render
function MissedGoal() {
return <h1>MISSED!</h1>;
}
function MadeGoal() {
return <h1>Goal!</h1>;
}
function Goal(props) {
const isGoal = props.isGoal;
if (isGoal) {
return <MadeGoal/>;
}
return <MissedGoal/>;
}
const details
={runs:13432,centuries:21,manofthematch=17,sixes=311}
const root = ReactDOM.createRoot(document.getElementById('root'));
Conditional Rendering
Using Ternary Operator
Another way to conditionally render elements is by using a ternary
operator( ?: )
condition ? true : false
function Goal(props) {
const isGoal = props.isGoal;
return (
<>
{ isGoal ? <h1>You Scored Goal</h1>:<h1>You Missed
Goal</h1>}
</>
);
}
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Goal isGoal={false} />);
Preventing Rendering
It might happen sometimes that we may not want some
components to render. To prevent a component from rendering we
will have to return null as its rendering output
function Message(props) {
if(!props.isVisible)
return null;
else
return <h1>{props.text}</h1>;
}
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Message text=“My Message Is Here”
isVisible={true}/>);