React JS Notes
React JS Notes
React JS Notes
Introduction:
React is JavaScript library created by Facebook.
Most popular JavaScript library for creating UI.
Also used by Netflix & Instagram.
Used to create Single Page Applications (SPA)
We can build modern, fast Single Page Applications or websites with
React.
What is a Library?
A library in programming can be explained as a collection of codes. We
use a library to write code in a much simpler way or to import a feature
from it into our project. JQuery is a library for example.
We can write JavaScript much simpler by using JQuery, or we can import
written JQuery features to our project. The project itself is not
dependent on a library.
What is a Framework?
A Framework, on the other hand, is a complete package of code with its
own functionalities & libraries. A Framework has its own rules, you don’t
have much flexibility and the project is dependent on the Framework
you use. Angular is an example of a framework.
React Installation:
React requires Nodejs.
After installing Nodejs, open your Terminal or Command Prompt and
type the following command to create your React app:
Both of the functions are valid React components. They may take props
as an argument (when necessary), but they must return a React
element.
IMPORTANT: Functional components are also known as stateless
components because, in the past, we couldn’t do more complex things
like React State (data) management or life-cycle methods in functional
components.
However, React introduced React Hooks in version 16.8, which now
allows us to use state & other features in functional components.
So a React Functional Component:
o is a JavaScript / ES6 function
o must return a React element
o take props as parameter if necessary
What is Props?
React is a component-based library which divides the UI into little
reusable pieces. In some cases, those components need to communicate
(send data to each other) and the way to pass data between components
is by using props.
“Props” is a special keyword in React, which stands for properties and is
being used for passing data from one component to another.
But the important part here is that data with props are being passed in a
uni-directional flow. (one way from parent to child)
Furthermore, props data is read-only, which means that data coming
from the parent should not be changed by child components.
And that’s it! We’ve achieved to render the data coming from the parent
component.
let’s do the same for other child components:
As we can see, each ChildComponent renders now its own prop data. So
this is how we can use Props for passing data and converting static
components into dynamic ones.
Recap
Props stand for properties and is a special keyword in React
Props are being passed to components like function arguments
Props can only be passed to components in one-way (parent to child)
Props data is immutable (read-only)
Understanding State:
Props are only being used for passing data. They are read-only which
means that components receiving data by props are not able to change
it.
However, in some cases, a component may need to manipulate data and
that’s not possible with props.
So React provides another feature for data manipulation which is known
as State.
What is State?
State is a special object that holds dynamic data, which means that
state can change over time and anytime based on user actions or certain
events.
State is private and belongs only to its component where defined,
cannot be accessed from outside, but can be passed to child components
via props.
State is initialized inside its component’s constructor method.
When a change in the state is made, state shouldn’t be modified
directly. Instead, state updates should be made with a special method
called setState( ).
State should not be overly-used in order to prevent performance
problems.
and later we can render the properties of the state object with
JavaScript’s dot notation, inside the render ( ) method:
Updating the State
A Component’s state can change under some circumstances like a
server response or user interaction (clicking on a button, scrolling
the page etc).
So when data changes, when a change in the state happens, React
takes this information and updates the UI.
The important point here is that we should not modify the state
directly.
Do Not Modify State Directly — React Official Docs
Using setState( )
Below you can see the right way of state changes in React:
The reason why we should use setState( ) is that because it’s the only
way to notify React for data changes. Otherwise React won’t be notified
and won’t be able to update the UI.
Array.map() method
The map() method is one of the most useful and often used.
It calls the function for each element of the array and returns the array
of results.
The map() method calls the provided function once for each element
in an array, in order.
Note: map() does not execute the function for array elements
without values.
Note: this method does not change the original array.
let users = [
{id: 1, name: "John"},
{id: 2, name: "Pete"},
{id: 3, name: "Mary"}
];
// returns array of the first two users
let someUsers = users.filter(item => item.id < 3);
console.log(someUsers.length); // 2
Lifecycle methods:
Every component of React application goes through some phases during
the life cycle.
There are main 3 lifecycle phases of components:
o Mount phase
o Update phase
o Unmount phase
Mount Phase:
This is a phase where component instance is created and inserted into
the DOM.
Mount phase has 3 life cycle methods: constructor, render,
componentDidMount. React will call these methods in the same
sequence.
componentDidMount()
Whenever this method is called, React has already rendered our
component and put it into the DOM. Therefore, if there is any
initialization you want to perform that relies on the DOM, do it here
and now.
State: You can set the state with this.setState(). Whenever you do this,
it will also trigger a re-render of the component.
Use Cases: You can use componentDidMount to fetch data from a
server with AJAX calls. We can add event listeners inside
componentDidMount.
Update Phase:
If props or state of a component are changed for whatever reason, an
update of the component is performed. However, this means that the
component has to be re-rendered.
In this phase, we have 2 lifecycle methods, render and
componentDidUpdate.
Routing:
What is react router?
React router is a routing library built on top of the react which is used to create the
routing in react apps.
What is Switch?
<Switch> returns only one first matching route.
Switch component helps us to render the components only when path matches
otherwise it fallbacks to the not found component.
URL Parameters
URL parameters helps us to render the same component based on its dynamic url.
<Route path="users/:id" component={Users} />
How to access route parameters:
this.props.match.params.id
NavLink:
It is used to style the active routes so that user knows on which page he or she is
currently browsing on the website.
Programmatically navigate
What is Programmatic navigation?
It means we need to redirect the user when an event happens on that route.
For example, when a user is successfully logged in he or she will be redirected to the
home page.
1) Create mapDispatchToProps.
2) It is function that accepts dispatch as an argument and returns a function
which is responsible for dispatching an action.
3) Dispatch function is getting action from action creator.
4) Action Creator: It is a callback function that returns an action.
It is a place where we have to make asynch calls and once the operation is
done, we have to continue dispatching an action.