React Notes
React Notes
In React application, there are several files and folders in the root
directory. Some of them are as follows:
Features of react:-
7.JSX
8.Components
5.Simplicity
Virtual Dom:-
React finds out what changes have been made, and changes only what needs
to be changed.
Upgrade to React 18
npm i react@latest react-dom@latest
// Before
// After
root.render(<App />);
The purpose of the function is to define the HTML element where a React
component should be displayed.
The render() method is then called to define the React component that should
be rendered.
It does NOT have to be a <div> element and it does NOT have to have
the id='root':
React JSX
JSX stands for JavaScript XML.
JSX follows XML rules, and therefore HTML elements must be properly closed.
The class attribute is a much used attribute in HTML, but since JSX is rendered
as JavaScript, and the class keyword is a reserved word in JavaScript, you are
not allowed to use it in JSX.
React Components
Components are independent and reusable bits of code. They serve the same
purpose as JavaScript functions, but work in isolation and return HTML.
When creating a React component, the component's name MUST start with an
upper case letter.
Class Component
The component also requires a render() method, this method returns HTML.
render() {
Function Component
A Function component also returns HTML, and behaves much the same way as a
Class component, but Function components can be written using much less
code, are easier to understand,
Class Component:-
When creating a React component, the component's name must start with an
upper case letter.
The component also requires a render() method, this method returns HTML.
render() {
Component Constructor
The constructor function is where you initiate the component's properties.
constructor() {
super();
render() {
Use an attribute to pass a color to the Car component, and use it in the render()
function:
render() {
}
}
root.render(<Car color="red"/>);
The state object is where you store property values that belongs to the
component.
When a value in the state object changes, the component will re-render,
meaning that the output will change according to the new value(s).
constructor(props) {
super(props);
this.state = {
brand: "Ford",
model: "Mustang",
color: "red",
year: 1964
};
changeColor = () => {
this.setState({color: "blue"});
render() {
return (
<div>
<h1>My {this.state.brand}</h1>
<p>
It is a {this.state.color}
{this.state.model}
from {this.state.year}.
</p>
<button
type="button"
onClick={this.changeColor}
>Change color</button>
</div>
);
Lifecycle of Components
Each component in React has a lifecycle which you can monitor and manipulate
during its three main phases.
Mounting
Mounting means putting elements into the DOM.
1. constructor:-
2.getDerivedStateFromProps
This is the natural place to set the state object based on the initial props.
3.render
The render() method is required, and is the method that actually outputs the
HTML to the DOM.
4.componentDidMount
This is where you run statements that requires that the component is already
placed in the DOM.
Updating:-
The next phase in the lifecycle is when a component is updated.
React has five built-in methods that gets called, in this order, when a
component is updated:
1.getDerivedStateFromProps
This is still the natural place to set the state object based on the initial prop
2.shouldComponentUpdate
3.render
The render() method is of course called when a component gets updated, it has
to re-render the HTML to the DOM, with the new changes.
4.getSnapshotBeforeUpdate
5.componentDidUpdate
Unmounting
The next phase in the lifecycle is when a component is removed from the DOM,
or unmounting as React likes to call it.
React has only one built-in method that gets called when a component is
unmounted:
React Props
Props are arguments passed into React components.
Props are also how you pass data from one component to another, as
parameters.
If you have a variable to send, and not a string as in the example above, you
just put the variable name inside curly brackets:
Note: React Props are read-only! You will get an error if you try to change their
value.
React Events
ust like HTML DOM events, React can perform actions based on user events.
React has the same events as HTML: click, change, mouseover etc.
Adding Events
React events are written in camelCase syntax:
unction Football() {
const shoot = () => {
alert("Great Shot!");
return (
);
root.render(<Football />);
Passing Arguments
To pass an argument to an event handler, use an arrow function.
function Football() {
alert(a);
}
return (
);
root.render(<Football />);
if Statement
We can use the if JavaScript operator to decide which component to render.
unction Goal(props) {
if (isGoal) {
return <MadeGoal/>;
return <MissedGoal/>;
function Garage(props) {
return (
<>
<h1>Garage</h1>
</h2>
</>
);
Ternary Operator
Another way to conditionally render elements is by using a ternary operator.
function Goal(props) {
return (
<>
{ isGoal ? <MadeGoal/> : <MissedGoal/> }
</>
);
React Lists
In React, you will render lists with some type of loop.
Keys
Keys allow React to keep track of elements. This way, if an item is updated or
removed, only that item will be re-rendered instead of the entire list.
function Car(props) {
}
function Garage() {
const cars = [
];
return (
<>
<ul>
</ul>
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);
React Forms
Handling forms is about how you handle the data when it changes value or gets
submitted.
When the data is handled by the components, all the data is stored in the
component state.
You can control changes by adding event handlers in the onChange attribute.
We can use the useState Hook to keep track of each inputs value and provide a
"single source of truth" for the entire application.
You can control the values of more than one input field by adding
a name attribute to each element.
function MyForm() {
event.preventDefault();
alert(inputs);
return (
<form onSubmit={handleSubmit}>
<input
type="text"
name="username"
value={inputs.username || ""}
onChange={handleChange}
/>
</label>
<input
type="number"
name="age"
value={inputs.age || ""}
onChange={handleChange}
/>
</label>
<input type="submit" />
</form>
root.render(<MyForm />);
function MyForm() {
);
const handleChange = (event) => {
setTextarea(event.target.value)
return (
<form>
</form>
root.render(<MyForm />);
In React, the selected value is defined with a value attribute on the select tag:
function MyForm() {
setMyCar(event.target.value)
return (
<form>
<option value="Ford">Ford</option>
<option value="Volvo">Volvo</option>
<option value="Fiat">Fiat</option>
</select>
</form>