Components
Components
Earlier the developers had to write 1000 lines of code for developing a simple single
page application. Most of those applications followed the traditional DOM structure and
making changes to them was very challenging and a tedious task for the
developers. They manually had to search for the element which needed the change and
update it accordingly. Even a small mistake would lead to application failure. Moreover,
updating DOM was very expensive. Thus, the component-based approach was
introduced. In this approach, the entire application is divided into logical chunks which
are called the Components. React was one of the frameworks who opted for this
approach.
React components are considered as the building blocks of the User Interface. Each of
these components exists within the same space but execute independently from one
another. React components have their own structure, methods as well as APIs. They are
reusable and can be injected into interfaces as per need. To have a better
understanding, consider the entire UI as a tree. Here the starting component becomes
the root and each of the independent pieces becomes branches, which are further
divided into sub-branches.
This keeps our UI organized and allows the data and state changes to logically flow from
the root to branches and then to sub-branches. Components make calls to the server
directly from the client-side which allows the DOM to update dynamically without
refreshing the page. This is because react components are built on the concept of AJAX
requests. Each component has its own interface that can make calls to the server and
update them. As these components are independent of one another, each can refresh
without affecting others or the UI as a whole.
1
2 import React from 'react';
3 import ReactDOM from 'react-dom';
4
5 class MyComponent extends React.Component{
render(){
6 return(
7 <div>
8 <h1>Hello</h1>
9 <h1>This is a Component</h1>
10 </div>
);
11 }
12 }
13 ReactDOM.render(
14 <MyComponent/>, document.getElementById('content')
);
15
16
GET REACT CERTIFIED TODAY!
Now that you have understood what is a component and what are its advantages, let’s
now find out how to feed data to these components.
1. Props
2. States
Props
Props stand for Properties. They are the read-only components which work similar to the
HTML attributes. Prop is a way of passing data from parent to child component. Let’s
understand this with an example.
As we already know, the react components arrange the UI in the form of a tree where
the parent component becomes the root and child components become branches and
sub-branches. Now suppose parent component wants to send data to one of its deeply
nested components. Let us say from component 1 you need to send a property to
component 6. How will you do that?
You cannot pass down a property directly to the target component. This is
because React follows the rule where properties have to flow down from a parent
component to an immediate child component. This means you can’t skip a layer of child
components when sending a property and the child components can’t send property
back up to a parent as well. You can have default props in case a parent component
doesn’t pass down props so that they are still set. This is why React has one-way data
binding.
So, in this case, we need to send data, layer by layer until it reaches target child
component. Every component in this path has to receive the property from its parent
and then resend that property to its child as received. This process repeats until your
property reaches its target component.
States
Generally, components take in props and render them. These are called stateless
components. But they can also provide state which are used to store data or
information about the component which can change over time. Such components are
called stateful components. The change in state can happen as a response to user
event or system event. In other words, state is the heart of every react component which
determines how the component will behave and render. They are also responsible for
making a component dynamic and interactive. Thus they must be kept as simple as
possible.
The state can be accessed with this reference, e.g., this.state. You can access and print
variables in JSX using curly braces {}. Similarly, you can
render this.state inside render(). You must set a default state for the component else it will
set to null.
1
2
3 import React from 'react';
4 import ReactDOM from 'react-dom';
5
class MyComponent extends React.Component {
6 constructor() {
7 super();
8 this.state = {
9 name: 'Maxx',
10 id: '101'
}
11 }
12 render()
13 {
14 setTimeout(()=>;{this.setState({name:'Jaeha', id:'222'})},2000)
return (
15
<div>
16 <h1>Hello {this.state.name}</h1>
17 <h2>Your Id is {this.state.id}</h2>
18 </div>
19 );
}
20 }
21 ReactDOM.render(
22 <MyComponent/>, document.getElementById('content')
23 );
24
25
States vs Props
GET TRAINED BY EXPERTS!
React provides various methods which notify when a certain stage in the lifecycle of a
component occurs. These methods are called the lifecycle methods. These lifecycle
methods are not very complicated. You can think of these methods as specialized event
handlers that are called at various points during a components life. You can even add
your own code to these methods to perform various tasks. Talking about the lifecycle of
the component, the lifecycle is divided into 4 phases. They are:
a. Initial Phase
b. Updating Phase
c. Props change Phase
d. Unmounting Phase
Each of these phases contains some lifecycle methods which are specific only to them.
So let’s now find out what happens during each of these phases.
a. Initial Phase – The first phase of the lifecycle of a React component is the initial
phase or initial rendering phase. In this phase, the component is about to start its
journey and make its way to the DOM. This phase consists of the following methods
which are invoked in a predefined order.
b. Updating Phase – Once the component is added to the DOM, they can update and
re-render only when a state change occurs. Each time the state changes, the
component calls its render() again. Any component, that relies on the output of this
component will also call its render() again. This is done, to ensure that our component is
displaying the latest version of itself. Thus to successfully update the components state
the following methods are invoked in the given order:
c. Props Change Phase – After the component has been rendered into the DOM, the
only other time the component will update, apart from the state change is when its prop
value changes. Practically this phase works similar to the previous phase, but instead of
the state, it deals with the props. Thus, this phase has only one additional method from
the Updating Phase.
ii. shouldComponentUpdate()
iii. componentWillUpdate()
iv. render()
v. componentDidUpdate()
d. The Unmounting Phase – This is the last phase of components life cycle in which
the component is destroyed and removed from the DOM completely. It contains only
one method: