Few Core Concepts in React JS

Short description

React JS

1. What is React?

React is an open-source frontend JavaScript library which is used for building user interfaces especially for single page applications. It is used for handling the view layer for web and mobile apps. React was created by Jordan Walke, a software engineer working for Facebook. React was first deployed on Facebook’s News Feed in 2011 and on Instagram in 2012.

2. What is JSX?

JSX is an XML-like syntax extension to ECMAScript (the acronym stands for JavaScript XML). Basically it just provides syntactic sugar for the React.createElement() function, giving us expressiveness of JavaScript along with HTML like template syntax.

In the example below text inside <h1> tag is returned as a JavaScript function to the render function.

3. What is the difference between Element and Component?

An Element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. Elements can contain other Elements in their props. Creating a React element is cheap. Once an element is created, it is never mutated.

The object representation of React Element would be as follows:

The above React.createElement() function returns an object:

And finally it renders to the DOM using ReactDOM.render():

Whereas a component can be declared in several ways. It can be a class with a render() method. Alternatively, in simple cases, it can be defined as a function. In either case, it takes props as an input, and returns a JSX tree as the output:

Then JSX gets transpiled to a React.createElement() function tree:

4. How to create components in React?

There are two ways to create a component.

  1. Function Components: This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as first parameter and return React elements:

2. Class Components: You can also use ES6 class to define a component. The above function component can be written as:

5. What is state in React?

State of a component is an object that holds some information that may change over the lifetime of the component. We should always try to make our state as simple as possible and minimize the number of stateful components.

Let’s create a user component with message state,

State is similar to props, but it is private and fully controlled by the component. i.e, It is not accessible to any component other than the one that owns and sets it.

6. What are props in React?

Props are inputs to components. They are single values or objects containing a set of values that are passed to components on creation using a naming convention similar to HTML-tag attributes. They are data passed down from a parent component to a child component.

The primary purpose of props in React is to provide following component functionality:

  1. Pass custom data to your component.
  2. Trigger state changes.
  3. Use via this.props.reactProp inside component's render() method.

For example, let us create an element with reactProp property:

This reactProp name then becomes a property attached to React's native props object which originally already exists on all components created using React library.

7. What is the difference between state and props?

Both props and state are plain JavaScript objects. While both of them hold information that influences the output of render, they are different in their functionality with respect to component. Props get passed to the component similar to function parameters whereas state is managed within the component similar to variables declared within a function.

8. What is Virtual DOM?

The Virtual DOM (VDOM) is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the “real” DOM. It’s a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called reconciliation.

9. How Virtual DOM works?

The Virtual DOM works in three simple steps.

  1. Whenever any underlying data changes, the entire UI is re-rendered in Virtual DOM representation.

2. Then the difference between the previous DOM representation and the new one is calculated.

3. Once the calculations are done, the real DOM will be updated with only the things that have actually changed.

10. What are Higher-Order Components?

A higher-order component (HOC) is a function that takes a component and returns a new component. Basically, it’s a pattern that is derived from React’s compositional nature.

We call them pure components because they can accept any dynamically provided child component, but they won’t modify or copy any behaviour from their input components.

HOC can be used for many use cases:

  1. Code reuse, logic and bootstrap abstraction.
  2. Render hijacking.
  3. State abstraction and manipulation.
  4. Props manipulation.

--

--

I am Aiman. I am a web developer. I can learn anything at any time. For that, I take the help of Google. My core skill is based on JavaScript .

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahad Hossain Aiman

I am Aiman. I am a web developer. I can learn anything at any time. For that, I take the help of Google. My core skill is based on JavaScript .