← Back to list

React —  A Modern Evolution of Web Development

1- Introduction to React

Urooj Arif · 2024-08-25 12:12 · 4 claps · 7.2 min read
#react #100daysofcode #100daysofbytewise #frontend-development #bytewise-fellowship
Open on Medium ↗
Wiki topics: 🌐 · Web Development

React — A Modern Evolution of Web Development

1- Introduction to React

What is React?

React is an open-source JavaScript library used for building user interfaces, particularly single-page applications where a fast, interactive user experience is required. It’s maintained by Facebook and a community of developers. React allows developers to build web applications that can update and render efficiently as the data changes.

History and Evolution

React was first developed by Jordan Walke, a software engineer at Facebook, and was initially released in May 2013. React brought a new approach to developing web interfaces by introducing the concept of components and a virtual DOM, which enhanced performance and made the development process more efficient.

Why Use React?

  • Component-Based Architecture: React encourages developers to break down the UI into reusable components, making the code more manageable and easier to debug.
  • Virtual DOM: React uses a virtual DOM to minimize the number of costly DOM manipulations, leading to faster UI updates.
  • Strong Community: React has a large and active community, ensuring continuous improvement and a plethora of resources for learning.
  • Flexibility: React can be used in various environments, including client-side, server-side, and even mobile apps with React Native.

2- Setting Up a React Environment

Installing Node.js and npm

To get started with React, you need to install Node.js, which comes with npm (Node Package Manager). These tools allow you to manage packages and run React applications.

  1. Download Node.js from the official website.
  2. Install it on your machine, which will also install npm.

Setting Up a React Project Using Create React App

Create React App is a command-line tool that sets up a new React project with a pre-configured development environment.

npx create-react-app my-app
cd my-app
npm start

This command creates a new directory called my-app, sets up the project, and starts the development server.

Understanding the Folder Structure

The project created by Create React App includes several key folders:

  • **public/**: Contains static files like index.html.
  • **src/**: Contains the React components and application logic.
  • **node_modules/**: Contains all the installed npm packages.

3- Core Concepts of React

Components

React applications are built using components, which are reusable pieces of UI. There are two types of components in React:

  • Functional Components: These are simple functions that return JSX.
  • Class Components: These are ES6 classes that extend React.Component and have more features, like lifecycle methods.
// Functional Component
const MyComponent = () => {
  return <h1>Hello, world!</h1>;
};

// Class Component
class MyComponent extends React.Component {
  render() {
    return <h1>Hello, world!</h1>;
  }
}

JSX (JavaScript XML)

JSX is a syntax extension for JavaScript that looks similar to HTML and is used to describe what the UI should look like. It’s not required in React but is widely used for its readability and ease of use.

const element = <h1>Hello, world!</h1>;

Props and State

  • Props: Short for properties, props are read-only inputs passed to a component from its parent.
  • State: State is a dynamic data structure that holds information that may change over time. Unlike props, state is managed within the component.
const Welcome = (props) => {
  return <h1>Hello, {props.name}</h1>;
};

Event Handling

React uses camelCase syntax for event handling and allows you to pass functions as event handlers.

const handleClick = () => {
  console.log('Button clicked');
};

<button onClick={handleClick}>Click me</button>;

Lifecycle Methods

img by Projects.wojtekmai

img by Projects.wojtekmai

Lifecycle methods are functions that get called at different stages of a component’s life, like when it mounts, updates, or unmounts. These are mainly used in class components.

class MyComponent extends React.Component {
  componentDidMount() {
    // Runs after the component is added to the DOM
  }

  componentWillUnmount() {
    // Runs before the component is removed from the DOM
  }

  render() {
    return <div>My Component</div>;
  }
}

4- Advanced React Concepts

React Hooks

Hooks were introduced in React 16.8 and allow you to use state and other React features in functional components.

  • useState: Allows you to add state to functional components.
  • useEffect: Allows you to perform side effects in functional components.
import { useState, useEffect } from 'react';

const Counter = () => {
  const [count, setCount] = useState(0);

  useEffect(() => {
    document.title = `Count: ${count}`;
  }, [count]);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
};

Context API

The Context API allows you to share state across multiple components without passing props down manually at every level.

const ThemeContext = React.createContext('light');

const App = () => {
  return (
    <ThemeContext.Provider value="dark">
      <Toolbar />
    </ThemeContext.Provider>
  );
};

React Router

In React, a DOM router typically refers to the routing mechanism provided by the popular library, React Router. React Router is used to handle navigation between different views or pages in a React application without requiring full-page reloads. It is specifically designed to work with single-page applications (SPAs).

Here is a basic overview to install and setup router:

npm install react-router-dom

Basic Setup: Here’s a basic example of how to set up routing in a React app:

import React from "react";
import { BrowserRouter as Router, Route, Routes, Link } from "react-router-dom";

function Home() {
  return <h2>Home Page</h2>;
}

function About() {
  return <h2>About Page</h2>;
}

function App() {
  return (
    <Router>
      <nav>
        <Link to="/">Home</Link> | <Link to="/about">About</Link>
      </nav>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </Router>
  );
}

export default App;

Explanation:

  • Router (<BrowserRouter>): Wraps your application and enables routing.
  • Routes (<Routes>): Defines all the available routes in your app.
  • Route (<Route>): Maps a specific path to a component. In the example above:
  • path="/" loads the Home component.
  • path="/about" loads the About component.
  • Link (<Link>): Provides navigation links that change the URL without reloading the page.

5- Styling in React

Inline Styles

Inline styles in React are written as objects, with camelCase properties.

const style = {
  color: 'blue',
  fontSize: '20px',
};

const MyComponent = () => {
  return <h1 style={style}>Hello, world!</h1>;
};

CSS Modules

CSS Modules allow you to scope CSS by automatically generating unique class names.

import styles from './MyComponent.module.css';

const MyComponent = () => {
  return <h1 className={styles.title}>Hello, world!</h1>;
};

Styled-components

Styled-components is a library that uses tagged template literals to style components.

npm install styled-components
import styled from 'styled-components';

const Title = styled.h1`
  color: blue;
  font-size: 20px;
`;

const MyComponent = () => {
  return <Title>Hello, world!</Title>;
};

CSS-in-JS

CSS-in-JS refers to a styling strategy where CSS is composed using JavaScript, often using libraries like Emotion or Styled-components.

6- Handling Forms in React

Controlled vs. Uncontrolled Components

  • Controlled Components: Form data is handled by the React component using state.
  • Uncontrolled Components: Form data is handled by the DOM itself.
// Controlled Component
const ControlledForm = () => {
  const [value, setValue] = useState('');

  const handleChange = (e) => {
    setValue(e.target.value);
  };

  return <input type="text" value={value} onChange={handleChange} />;
};

Form Validation

Form validation can be handled using native HTML validation attributes, custom validation logic in React, or libraries like Formik and Yup.

const handleSubmit = (event) => {
  event.preventDefault();
  if (!value) {
    alert('Input cannot be empty');
  }
};

Third-Party Form Libraries

Libraries like Formik simplify form management and validation in React.

npm install formik yup
import { Formik, Field, Form } from 'formik';
import * as Yup from 'yup';

const SignupSchema = Yup.object().shape({
  email: Yup.string().email('Invalid email').required('Required'),
});

const SignupForm = () => (
  <Formik
    initialValues={{ email: '' }}
    validationSchema={SignupSchema}
    onSubmit={(values) => {
      console.log(values);
    }}
  >
    {({ errors, touched }) => (
      <Form>
        <Field name="email" />
        {errors.email && touched.email ? <div>{errors.email}</div> : null}
        <button type="submit">Submit</button>
      </Form>
    )}
  </Formik>
);

7- React Performance Optimization

UseMemo and UseCallback

  • useMemo: Memoizes a computed value, only recalculating it when dependencies change.
  • useCallback: Memoizes a function, preventing it from being recreated on every render.
const MemoizedComponent = React.memo(({ count }) => {
  console.log('Rendering Memoized Component');
  return <div>{count}</div>;
});

const ParentComponent = () => {
  const [count, setCount] = useState(0);

  const increment = useCallback(() => {
    setCount(count + 1);
  }, [count]);

  return (
    <div>
      <button onClick={increment}>Increment</button>
      <MemoizedComponent count={count} />
    </div>
  );
};

Memoization Techniques

Memoization helps avoid expensive recalculations by caching the result of a function call. You can use libraries like memoize-one or React's built-in hooks like useMemo.

Avoiding Re-Renders

Re-renders can be avoided by using React.memo, PureComponent, or by managing state more efficiently. Avoid passing new object references as props unless necessary.

8- Building and Deploying React Applications

Building for Production

To create an optimized build of your React application, run the following command:

npm run build

This will create a build/ folder with your optimized production files.

Deploying to Netlify, Vercel, or GitHub Pages

React applications can be deployed easily to platforms like Netlify, Vercel, or GitHub Pages.

  • Netlify: Drag and drop your build/ folder in the Netlify dashboard, or connect your GitHub repository for continuous deployment.
  • Vercel: Install the Vercel CLI and deploy your app with a single command:
npm install -g vercel
vercel
  • GitHub Pages: Deploy to GitHub Pages using the gh-pages package:
npm install gh-pages --save-dev

Add the following to your package.json:

"scripts": {
  "predeploy": "npm run build",
  "deploy": "gh-pages -d build"
}

Then deploy with:

npm run deploy

Continuous Integration and Deployment (CI/CD)

CI/CD pipelines automate the testing and deployment of your application. Services like GitHub Actions, Travis CI, and CircleCI can be used to set up CI/CD pipelines for React applications.

Best Practices and Tips

Code Organization

Organize your code in a way that makes it easy to manage and scale. Some common structures include:

  • Feature-Based Structure: Group components, styles, and tests by feature.
  • Layer-Based Structure: Separate components, services, and utilities into different layers.

Accessibility in React

Make sure your React applications are accessible to all users, including those with disabilities. Use semantic HTML elements, ARIA attributes, and tools like eslint-plugin-jsx-a11y to ensure accessibility.

Writing Reusable Components

Focus on creating reusable components by making them flexible and configurable through props. Avoid hardcoding styles or logic that may need to be changed later.

Documentation and Comments

Good documentation and comments are essential for maintaining and scaling React applications. Use tools like Storybook to document your components, and write clear, concise comments in your code.

Conclusion

React is a powerful library that has transformed the way we build web applications. Its component-based architecture, combined with modern features like hooks, makes it a versatile and efficient tool for developers. As the React ecosystem continues to grow, staying updated with the latest best practices and tools is essential for building high-quality, maintainable applications.

Whether you’re just starting with React or you’re an experienced developer, there are always new techniques and tools to learn. Keep experimenting, building, and improving your React skills to stay ahead in the ever-evolving world of web development.

Happy Coding!


메타데이터
post_id
ff67bc9329dc
slug
react-a-modern-evolution-of-web-development-ff67bc9329dc
url
https://medium.com/@uroojarif479/react-a-modern-evolution-of-web-development-ff67bc9329dc
canonical_url
https://medium.com/@uroojarif479/react-a-modern-evolution-of-web-development-ff67bc9329dc
author_url
https://medium.com/@uroojarif479
status
ok
fetched_at
2026-08-06 21:52:48