Sitemap
Tolu Thakur

Tolu Thakur

REACT

5 min readNov 8, 2023

--

Press enter or click to view image in full size

Introduction to React

React is a popular JavaScript library for building user interfaces, especially for single-page applications where UI updates can happen without a page refresh. Developed and maintained by Facebook, React provides a component-based architecture, making it easy to build modular and reusable UI elements.

Key Concepts in React:

  1. Components
  2. virtual DOM
  3. JSX (JavaScript XML)
  4. state and props
  5. unidirectional data flow
  6. lifecycle method (for class components )
  7. react hooks

Setting up a react application

Press enter or click to view image in full size

To start using React, you’ll need Node.js and npm (Node Package Manager) installed on your system. You can create a new React application using Create React App, a popular toolchain for setting up a new React project with a single command:

By doing this, a development server will be started and a new React application will be created under the ‘my-react-app’ directory, enabling you to view your React app in a browser.

You’re all set to dive into React and start creating dynamic, interactive web application user interfaces using these fundamental ideas and resources!

Explain what React components are, component state, props, reusability of components, and how components make React modular.

1. React Components:

In React, components are the building blocks of user interfaces. A component is a self-contained, reusable module that renders a part of the UI. You can think of components as personalized HTML elements. They take in props as inputs and output React elements that describe what ought to show on the screen.

There are two types of components in React:

.Functional Components: These are JavaScript functions that return React elements and take an argument called props. Simpler and simpler to understand functional components are used.

Example :-

.Class Components: These are ‘React. Component’ extending ES6 classes. They have extra lifecycle techniques and the ability to hold and maintain local state.

2. Component State:

React components employ states to express mutable data that is subject to change. Using the ‘this.state’ object for class components or the ‘useState’ hook for functional components, a component can manage its state. React automatically re-renders a component to reflect its new state when its state changes.

Get Tolu Thakur’s stories in your inbox

Join Medium for free to get updates from this writer.

Example of using state in a functional component with 'useState':

Press enter or click to view image in full size

3. Props:

Props, which stands for properties, are a means of data flowing between parent and child components. They facilitate the reusability of components and are read-only. Similar to HTML attributes, props are supplied to components and are accessible within the component.

Example of using props:

4. Reusability of Components:

React components are very reusable. Once a component is defined, you may use it repeatedly in your application to modify its behavior by feeding it new props. Codebase redundancy is decreased, consistency is encouraged, and development is made easier with reusability.

5. How Components Make React Modular:

React components promote modularity in several ways:

Encapsulation: It is simpler to reason about and manage individual sections of the application because components contain their own logic and user interface.

Composition:Complex user interfaces can be created by combining components. A more complex application can be divided into more manageable, smaller components, each in charge of a certain function.

Reusability: Component reuse increases the modularity of the codebase by allowing it to be utilized in multiple projects or even different sections of the application.

Separation of Concerns: The separation of responsibilities between various UI components facilitates easier management and modification of individual components without affecting the program as a whole.

Learn Hooks like useState, useEffect, useContext etc. Know when and why to use them.

Here are some key points about commonly used React Hooks and when to use them:

useState — For managing local state within a functional component. Gives access to state value and setState function to update it.

useEffect — Accepts a callback function that runs after render. Useful for side effects like API calls, subscriptions, manual DOM mutations etc.

useContext — Used to access context values instead of passing props down manually at every level. Avoid prop drilling.

useReducer — Alternative to useState if state logic gets complex. Used with a reducer function for updating state.

useCallback — Returns memoized callback to optimize performance on re-renders. Use if passing callbacks to optimized child components.

useMemo — Returns memoized value to avoid expensive calculations on every render. Use if doing heavy computations in render.

useRef — Gives access to a mutable ref object. Commonly used to access DOM elements directly.

useImperativeHandle — Allows exposing custom instance values to parent component.

useLayoutEffect — Identical to useEffect but runs synchronously after render. Use for DOM mutations or visible UI updates.

useDebugValue — Used to display a label in React DevTools for custom hooks.

React Hooks allow you to leverage React features without writing a class component. Know when to use each one based on what you need to implement.

--

--