React Notes
React Notes
Components are building block of apps. Components describe a part of the user
interface. They are reusable and can be nested inside other components.
Like header, footer, side nav, main content they all form part of root components which
is often called as app component.
Types
1. Functional component
Simple functions
Use functional component as much as possible
Absence of this keyword
State full
Example
2. Class component
JSX
const Hello=()=>{
return (
<div className="DummyClass">
<h1>Hello Naveed Akram</h1>
</div>
)
}
WITHOUT JSX
const Hello=()=>{
return React.createElement("div",
{id: 'Hello' , className: 'dummyclass'},React.createElement('h1',null,'hello
Naveed Akram'))
Properties (Props)
Props are optional input which your component can accept. Props is object and has
attributes passed from parent component. Props are immutable (their value cannot be
changed).
const Greet=(props)=>{
console.log(props)
return(
<div>
<h1>Hello {props.name} a.k.a {props.heroName}
{props.childern}
</h1>
</div>
)
}
export default Greet
Props State
Props get passed to the component State is managed within the component
Functions parameters Variables declared in the function body
Props are immutable State can be changed
Props –functional components UseState Hook – Functional Components
this.props – Class Components this.state –Class Components
Example Code
</div>
)
}
}
export default Message;
SETSTATE
SetState is used to update the value of GUI using some method and changing it on any
event like click etc.
DO’s
Always make use of setState and never modify the state directly.
Code has to be executed after the state has been updated? Place that code in
the call back function which is the second argument to the setState method.
When you have to update state on the previous state value, in a function as an
argument instead of the regular object.
Example Code
render() {
return (
<div>
<div>count - {this.state.count}</div>
<button onClick={()=>this.IncrementFive()}>Increment</button>
</div>
)
}
}
export default Counter;
const Greet=(props)=>{
const {name,heroName}=props
return (
<div>
<h1>Hello {name} a.k.a {heroName}
</h1>
</div>
)
}
Event Handling on JS
In Classes
return (
<div>
<button onClick={this.clickHandler}>Click Me</button>//()no brackets in
front of method
</div>
)
}
}
In Functions
function FunctionClick() {
function clickHandler(){
console.log("I am Clicked")
}
return (
<div>
<button onClick={clickHandler}>Click</button>//onClick in camelCase
</div>
)
}
1. In render method
4. In class as arrow
this.state = {
message:"Hello"
}
this.clickHandler=this.clickHandler.bind(this)//Binding in class
constructor
}
// clickHandler(){//Best option uptill now
// this.setState({//Changing state
// message:"Good Bye"
// })
// }
clickHandler=()=>{//Another approach to bind the event handling
this.setState({//Changing state
message:"Good Bye"
})
}
render() {
return (
<div>
<div>
{this.state.message}
</div>
<button onClick={this.clickHandler}>Click</button>
</div>
)
}
}
export default EventBind
Method as Props
Example of Parent
this.state = {
ParentName: 'Parent'
}
this.greetParent = this.greetParent.bind(this);//Event Binding
}
greetParent(childName){
alert(`Hello ${this.state.ParentName} from ${childName}`)
}
render() {
return (
<div>
<ChildComponent greetHandler={this.greetParent}/>
</div>
)
}
}
function ChildComponent(props) {
return (
<div>
<button onClick={()=>props.greetHandler('Child')}>greetParent</button>
</div>
)
}
Conditional Rendering
If/else
It is based on some decision making and then showing HTML depend on
condition which is true.
class UserGreeting extends Component {
constructor(props) {// Use rconst for construcor snippet
super(props);
this.state = {
IsLoggedIn: true,// defining a props
};
}
Elements variable
This approach is used in the form of a variable in which message is stored
in the form variable and then HTML is displayed from variable.
class UserGreeting extends Component {
constructor(props) {// Use rconst for construcor snippet
super(props);
this.state = {
IsLoggedIn: false,// defining a props
};
}
this.state = {
IsLoggedIn: false,// defining a props
};
}
)
}
}
function NameList() {
const names = ["Naveed", "Ali", "Ahmed"]; //declaring array
const NameList = names.map((name) => <h2>{name}</h2>); //applying map method
on to show list with h2 tag
return <div>{NameList}</div>; //Displaying namelist JSX
}
function NameList() {
const persons = [
{
rollno: 1,
name: "Naveed",
age: 30,
section: "A",
},
{
rollno: 2,
name: "ALI",
age: 20,
section: "B",
},
{
rollno: 3,
name: "Ahmer",
age: 25,
section: "C",
},
];
this.state = {
name: "naveed",
};
console.log("Life cycle Construtor A");
}
static getDerivedStateFromProps(props,state){
console.log('static getDerivedStatefromprops A')
return null
}
componentDidMount() {
console.log("Componet did mount A");
}
render() {
console.log("Render method A");
return(
<div>LifeCycleA
<LifeCycleB/>
</div>
)
}
}
this.state = {
name: "naveed",
};
console.log("Life cycle Construtor A");
}
static getDerivedStateFromProps(props,state){
console.log('static getDerivedStatefromprops A')
return null
}
componentDidMount() {
console.log("Componet did mount A");
}
shouldComponentUpdate(nextProps,nextState){
console.log("shouldComponentUpdate from A")
return true
}
getSnapshotBeforeUpdate(prevProps, prevState ){
console.log('getSnapshotBeforeUpdate from A')
return null
}
componentDidUpdate(prevProps,prevState,snapshots){
console.log('componentDidUpdate from A')
}
changeState=()=>{
this.setState({
name:"React course"
})
}
render() {
console.log("Render method A");
return(
<div>
<div>LifeCycleA</div>
<button onClick={this.changeState}>Change on CLick</button>
<LifeCycleB/>
</div>
)
}
}
because it memoizes the result and skips rendering to reuse the last rendered result.
When to Use it
Use React Memo if your component will render quite often.
Use it when your component often renders with the same props. This happens to
child components who are forced to re-render with the same props whenever the
parent component renders.
Use it in pure functional components alone. If you are using a class component,
use the React.PureComponent.
Use it if your component is big enough (contains a decent amount of UI
elements) to have props equality check.
Syntax
import React from 'react'