React Fundamentals
React Fundamentals
Introduction
React is a Javascript library focused on building rich user interfaces. It is not a
framework.
It does not handle http and requests , but has a rich ecosystem to get that work done. It has a
component based architecture.
React follows a Declaritive paradigm instead of the conventional Imperative paradigm.
1. Using npx :✔
Node Package Runner, which gets installed while installing Node It
allows to directly run create-react-app without installing it
Use command: npx create-react-app project_name
2. Using npm :
It requires to install the create-react-app globally and then use it to generate projects Use
command: npm install create-react-app -g
and then create-react-app project_name
Folder Structure
Creating a react project comes with some files, which we need to understand. Following are the files which we need to know for every react
project we create.
package.json : This files contains the dependencies and scripts required for the project.
node_modules : This folder contains all the installed dependencies. It is generated either when you run create-react-appcommand or
when you run npm install command.
index.html : This is the only .html file in our application as react is a single page application. This is the entrypoint which
is served on the client side and typically no code is added to this file.
src : This is the folder where the actual code is written and organized.
index.js : This is the very first file which is called by the entry file i.e index.html .
App.js : This is the root component of our react app. All the other components are enclosed within this component. It is
responsible for all the html displayed on the browser. index.js imports this App component.
App.css : Global styling options are written in this file. Eg. App theme can be written in this file.
Virtual DOM
React uses the virtual DOM to clone a node that already exists in the DOM
Subtrees are created in the virtual DOM and then rendered based on state changes When a state
Then it updates the DOM based on the result of that diff (Referred to as reconciliation)
The Virtual DOM is considered the magic behind React. It batches DOM operations to keep everything quick and snappy because DOM
manipulation is SLOW
Once a state is changed it triggers the diff algorithm to check all components, re-rendering only those that have changed properties.
JSX
JSX is a JavaScript Extension Syntax used in React to easily write HTML and JavaScript together
To convert JSX into browser understandable JavaScript code, we use a tool like Babel which is a JavaScript compiler/transpiler.
Let's see the breakdown of the JSX converting process to React.createElement function: E.g
JSX Code:
compiles to
React.createElement("h1",
{className:"topBarHeading", "I am Himanshu"
})
Components
The entire react app is made up of small individual modules called as Components. The App
component is the root component ( App.js )
It contains all the other component.
Naming Convention: Components are always named in Pascal Case i.e the starting character is always in uppercase. Components are
resuable.
That means, same components can be reused with different props (properties) and state (data) to display different information.
Component can contain another component.
Eg:
All these are individual components are enclosed in the App component which is the root component.
A component code is placed inside a Javascript file.✔
However, we can also have seperate components files with .jsx extension. There are two
types of Components:
Functional Components
Class Components
Functional Component
Similar to a function and returns jsx that gets rendered to the DOM
//Searchbar.js
const Searchbar = () =>
{return <input />;
};
//index.js
const App = () =>
{return (
<div>
<Searchbar
/>
</div>
);
}
It is self aware and keeps track of anything that happens to it once it has been rendered It is created
By extending React.Component you give the Searchbar class added functionality (state and props)
To call an instance of a class you have to wrap it in jsx tags E.g: <Searchbar />
Start off with a functional component and as your component gets complex you can convert to a class
In a class component props are available within the component and don't need to be passed in as an argument. It can be accessed in
a method using this.props
Event Handling
Similar to a regular event handler, in react it is a function that runs anytime an event occurs
In react you declare the event handler then pass it to the element that you want to monitor for events It is
considered best practice to begin the name of an event handler with on or handle E.g:
Whenever state is changed a component re-renders and also forces all of its children to re-render as well
Before state can be used in a component the object needs to be initialized E.g:
It is reserved for setting up certain configurations in a class such as initializing variables, initializing state or binding event handler
methods
super is a reference to the constructor method on the React.Component class that is getting extended. This means that is
has its own constructor function and so to reference it we have to use the super keyword
In the code above state has been initialized E.g: this.state = { property1: '' }
This syntax should only be used to declare state within the constructor function
this.setState() should be used to manipulate state after it has been initialized E.g this.setState({ property1:
'new state'});
This means that the component is declarative, the state determines how the data or UI is manipulated.
When an action (or change) occurs the component is already aware through its change in state and then re-renders
Component Lifecycle
shouldComponentUpdate
getDerivedStateFromProps
getSnapshotBeforeUpdate
Event Handlers: Handling events with React elements is very similar to handling events on DOMelements
React Refs
Gives access to a single DOM element
We create refs in the constructor -> Assign them to instance variables -> then pass to a particular JSX element as props
React Hooks
Hooks System
Hooks are way to write resuable code, instead of more classic techniques like inheritance
useState
useEffect
useContext
useReducer
useCallback
useMemo
useRef Custom
hook
Redux
React is to render and present data to user not handling
What is Redux?
State Managment Library
Makes creating complex applications easier Not
required to create a React App!
Not explicitly designed to work with React!
Redux Cycle
Action Creator
⬇
Action
⬇
Dispatch
⬇
Reducers
⬇
State