What Is a React Component?

Written by promotiondept

April 20, 2021

What Is a React Component? React is one of the most popular JavaScript libraries for building user interfaces. If you are thinking of getting into front-end development or web development, then learning React will be a good way to start.  In this blog you are going to learn bASIc concepts in React such as components and their types, JSX syntax and props. Let’s start with JSX.  Soon after installing a React […]

Joy is for the individuals who plan well and seek after. A significant among us have been demonstrated the individuals who have genuine dream to live for likely REALIZE IT. It is just the individual pursuing the DREAM days and night until achievement. There is an expression of proficiency a head of you. Steps and obstructions from the outset appear to be enormous. Be that as it may, just for certain occasions those troublesome advances and difficulties are so natural execution for you. There are too a lot of instruments including VISUALIZATIONS and helps are around you. Pete Tran is here my adored. These are the devices to understand ANY of your HIGH DREAMS come True. I went additional miles for you. It I a take for you. They are the 'Enchantment WONDERS" you can call on. Happiness will be with you. Worldwide has improved numerous perspectives this season of return. Do you recall.. these means well? It will be my actual satisfaction too!

Found great life changing solutions

a React Component?

React is one of the most popular libraries for building user interfaces. If you are thinking of getting into front-end development or web developmentthen learning React will be a good way to start. 

In this blog you are going to learn basic concepts in React such as components and their types, JSX syntax and props. Lets start with JSX. 

Soon after installing React project, if you open App.js in your favourite editor you will orve that a function returns some HTML code. It is called JSX(JavaScript XML). What are the components in React JS

This is a valid and widely accepted syntax extension to . It is recommended to use it to describe what the UI should look like 

In frontend-related projects, we keep HTML, CSS, and code in separate files. However, React works a bit differently. In React we don’t create HTML files because JSX allows us to write HTML and  combined in the same file. But CSS needs to be separated to a new file as shown in the figure (App.css). 

What are the components in React JS

We can assign HTML tags to  variables: 

Code: 

What are the components in React JS

 Here, we have assigned HTML to name variable. 

You can return HTML conditionally (such as if-else cases): 

Code:

What are the components in React JS

Read more about JSX here

There are some differences as compared to HTML: 

Code:

What are the components in React JS

A component is an independent, reusable bit of code which divides the UI into smaller pieces. For example, if we were building the UI of React website using Reactjs we can break its UI into smaller parts as shown in the figure.

What is Component

Instead of building the whole UI under one single file like HTML, we can divide all the sections (marked with red) into smaller independent pieces. In other words, these are components. Each component will go into its own  file. 

In React we have two types of components: functional and class. Let’s look at each type of component in more detail. But before that, lets gain an understanding of extraction of components. 

As you know, everything in React is a component. Each component returns some HTML but we can return only single HTML element; inside which we can write y child elements as we have seen in the examples. Instead of writing all the code in a single file we can split our app into multiple components. 

We can then extract the code which is used for some unique purpose into a separate component and use that component wherever we want in our app, instead of rewriting and duplicating the code. 

In the following examples we have already extracted components into 

Now I can use both the components in any place inside the app without duplicating the code. This is called extracting and reusing components 

The first, most important and recommended component type in React is functional component. A functional component is basically a function that returns a React element (JSX).  

Functional components start with function keyword. 

Code:

What are the components in React JS

Alternatively, you can also create a functional component with the arrow function syntax(ES6).

Code:  

What are the components in React JS

You need to export the component in order to use it somewhere else. 

Code: 

What are the components in React JS

As HelloWorld is exported herewe can import this component into another file (which will be App.js in our case) and use it as shown below. 

Code:

What are the components in React JS

In short, a functional component is a  function or ES6 function which takes props as arguments and returns a JSX. 

Components should always start with a capital letter. 

Another type of component is a class component. Just as we have functionswe also have classes in Class components are ES6 classes that return JSX. The same  HelloWorld function can be converted to class component. 

Class components start with class keyword that extends the Component tructor from React and has a render method which returns a JSX. 

Code: 

What are class components

Apart from returning the JSX, sometimes we have a situation where we need to age some data inside the components; which makes the components more dynamic and makes our application faster and more user friendly. This data is called state 

We used to use class components in order to age the stateIn the older versions of React (before version 16.8), it was not possible to use state inside functional components. 

Apart from the state we use class components to perform some additional operations, like life-cycle .  

To summarise, a class component is an ES6 class which extends a React.Component. It render method to return a JSX. It accepts props (inside tructor) if necessary. 

This has changed with the introduction of React Hooks, and now we can also age state and perform life cycle inside functional components with hooks.  

Prop is a way that components communicate. React  props to transfer data from one component to another component. 

But remember that props only transport data in a one-way flow (only from parent to child components). It is not possible for props to pass data from child to parent, or to components at the same level. 

Now lets go back to our App component and pass props from an App to its child (HelloWorld.js in our case).  

First, we need to define a prop on the HelloWorld Component and assign a value to it. 

Code:

What are the components in React JS

Here we passed title as a prop to HelloWorld component and gave it a value of “Hello World” which is a string. 

Props are custom values and they also make components more dynamic.  

Since the HelloWorld component is the child here, we need to define props on its parent (App), so we can pass the values and get the result simply by accessing the prop title in the HelloWorld component as shown below. 

Code:

What are the components in React JS

If you save the files and start the app and look at the browser, you will find HelloWorld on the screen as shown below. What are the components in React JS

Now we have made the HelloWorld component more dynamic by passing our title as a prop to the component. Let’s see how we made it dynamic. 

We can use HelloWorld component more than once in our App component but with different value than our title prop, as shown below. 

Code:

What are the components in React JS

Now if you save the files and reload the browser, the screen is shown as below.What are the components in React JS

Now we are outputting different titles from the same component (HelloWorld.js) which makes the component Dynamic. 

We can do the same using class components as well. Now lets change HelloWorld.js into class component which accepts props. We can achieve that by changing the code as shown below. 

Code:  

What are the components in React JS

Now when you save it, you will find no change in the output you see on the screen. But this time we are using class components. 

NoteWe can access props directly upon this keyword in class components (this.props.title) and there is no need of tructor. It has been added just for the reference, and we will learn about it next. 

It’s important to note that props are Read Only; that is, we cannot change the value of the props inside the child components. We can only use the props as shown in the example without changing their value. 

In the tructor is a method used to initialize an object’s state in a class. It is automatically called during the creation of an object in a class.  

The concept of a tructor is the same in React. The tructor in a React component is called before the component is mounted. We use that to define a state object inside a class component. 

We can change our existing class component to use state and props by changing the code as shown below. 

Code:

What are the components in React JS

Now if you save you will find no change in the output you see on the screen. But this time we are using state and props inside the class component. 

Sometimes when working with React, people get carried away and create too y components. This leads to deeply nested structures which make it  to pass props all the way down. This can be solved to an extent by using 2 techniques. 

In React, a composition is a pattern of the component model. It’s how we build components from other components, of varying complexity and specialization through props. Depending on how generalized these components are, they can be used in building y other components.  

The Provider component which is provided by React Redux and Context API in React Hooks is the best example of a Composition. 

For Example:  

Here we have App.js which passes title and description as props to HelloWorld.js component. 

Code:  

What are the components in React JS

HelloWorld.js  title prop but it does not use the description; instead, it just sends it as a prop to its child component, that is Description.js.

What are the components in React JS

Description.js actually  description which is passed from App.js. This is called prop drilling. 

This can be avoided by creating a context for our App and composing our app with the Provider component, which is provided by createContext hookand then passing our required data as an object to the provider component as shown below. 

Code:

What are the components in React JS

Now we can pull out our data from the context using a useContext hook, and use the required data within the components instead of passing them as props, as shown below. 

Example: In HelloWorld.js we pull out the title as we only need that in this component. 

Code: 

What are the components in React JS

Example:In HelloWorld.js we pull out the description as we only need that in this component. 

Code: 

What are the components in React JS

If you orve in the ae code, the components are not taking any props but are still getting access to all the dataas we have Composed our App component with the Provider component from context. This helps in less prop drilling, and cleaner and readable code. 

Higher Order Components: 

Higher Order Components are functions used for adding additional functionalities to the existing component. These functions are pure, which means they are receiving data and returning values according to that data. If the data changes, higher order functions are re-run with different data inputs. 

Higher Order Component (HOC) is wrapping around “normal” component and providing additional data input. It is actually a function that takes one component and returns another component that wraps the original one. 

Now let’s try to rewrite the App we used all this time using Higher Order Components. 

Let’s create a Higher Order Component  MyHOC.js which takes component and returns a component as shown below. 

Code: 

What are the components in React JS

Here our Higher Order Component returns a component. However, by enhancing its properties in this case we pass additional data (description) as prop, along with the props which are passed to the component. 

Now we can pass our HelloWorld.js to this Higher Order Component as shown below. 

Code:

What are the components in React JS?What are the components in React JS

Here we wrap the Helloworld.js within the Higher Order Component that we just created; but we can use the description prop passed by MyHOC component which is a Higher Order ComponentWe can avoid prop drilling, and add y other properties to other components using Higher Order Components. 

What have you learnt? 

We have learnt about the two types of components in React; functional and class components. We learnt about extraction of components and how to reuse the components. After that, we went on to learn about props: how to pass them and use them inside the child components. We understood what prop drilling is, and have seen how to avoid prop drilling by using composing method and Higher Order Components.  

If you are aspiring to be a React developer, these are basic concepts that you should thoroughly understand, before moving on to more complex code. 

  • Within JSX we can add (logic, functions, variables etc) as well by using curly braces { } inside the HTML as shown in the figure. 
  • We can assign HTML tags to  variables: 

  • You can return HTML conditionally (such as if-else cases): 

  • Some attributes like class become className (because class refers to classes), tabindex becomes tabIndex and should be written in Camel Case. 
  • We can’t return more than one HTML element at once, so make sure to wrap them inside a parent tag. 
  • HelloWorld.js: For rendering the title  
  • Description.js: for rendering the description 
  • Composition 
  • Higher Order Components 

  • Source

    How To Really REALIZE DREAMS COME TRUE?

    Happiness is for those who plan well and pursue. A profound among us have been proven those who have true dream to live for likely REALIZED IT. It is just simply the person working toward the DREAM days and night until accomplishment. There is a phrase of efficiency a head of you. Steps and obstacles at first seem tremendous. However, just with some times those difficult steps and challenges are so easy performance for you. There are also plenty of tools including VISUALIZATIONS and helps are around you.

    COVID-19 – Effective Tips For You!

    COVID-19
    HOW TO DEFEAT DEADLY CORONAVIRUS EVERY TIME?

    The Greatest Virus Surviving 10 Steps Guide

    Free Risks Helps & COVID-19 Solutions

    Here Are Great Free Tips For You.

    Please Order If See Anything You Need? Mutual Reciprocal & Have Great Days!

     

    Amazon Best Sellers

    How To Really REALIZE DREAMS COME TRUE?

    Congratulations

    COVID-19 – Effective Tips For You!

    COVID-19
    HOW TO DEFEAT DEADLY CORONAVIRUS EVERY TIME?

    The Greatest Virus Surviving 10 Steps Guide

    Free Risks Helps & COVID-19 Solutions

    Here Are Great Free Tips For You.

    Please Order If See Anything You Need? Mutual Reciprocal & Have Great Days!

     

    Amazon Best Sellers

    You May Also Like…

    0 Comments

    Submit a Comment