[go: up one dir, main page]

DEV Community

Mofajjal Rasul
Mofajjal Rasul

Posted on • Edited on

Getting Started with React.js: A Beginner's Guide

React.js is a popular JavaScript library for building user interfaces, particularly for single-page applications. Here’s a concise guide to help you kick-start your journey with React.

Core Concepts

1. Components

Components are the building blocks of a React application. They are reusable pieces of code that represent part of the UI. You can create components as either classes or functions.

Functional Component Example:

const Greeting = ({ name }) => {
  return <h1>Hello, {name}</h1>;
};
Enter fullscreen mode Exit fullscreen mode

Class Component Example:

class Greeting extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}
Enter fullscreen mode Exit fullscreen mode

2. JSX

JSX (JavaScript XML) allows you to write HTML-like syntax within your JavaScript code. It makes it easier to visualize the UI structure.

const element = <h1>This is JSX</h1>;
Enter fullscreen mode Exit fullscreen mode

JSX must be transpiled into regular JavaScript using tools like Babel.

3. Props

Props, or properties, are used to pass data from parent components to child components. They are immutable and help in creating dynamic UIs.

const App = () => {
  return <Greeting name="Alice" />;
};
Enter fullscreen mode Exit fullscreen mode

4. State

State is an object that holds data local to a component. Unlike props, state can be changed within the component, allowing for dynamic rendering.

Using State in a Functional Component (with Hooks):

import React, { useState } from 'react';

const Counter = () => {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>{count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

5. Component Lifecycle

Every component in React goes through a lifecycle that includes three phases: mounting, updating, and unmounting. Lifecycle methods allow you to hook into these phases.

Common Lifecycle Methods (for Class Components):

  • componentDidMount: Invoked immediately after a component is mounted.
  • componentDidUpdate: Invoked immediately after updating occurs.
  • componentWillUnmount: Invoked immediately before a component is unmounted and destroyed.

6. Hooks

Hooks are functions that allow you to use state and lifecycle features in functional components. The most common hooks are:

  • useState: For managing state.
  • useEffect: For side effects like data fetching and subscriptions.

Example of useEffect:

import React, { useEffect } from 'react';

const FetchData = () => {
  useEffect(() => {
    fetch('/api/data')
      .then(response => response.json())
      .then(data => console.log(data));
  }, []); // Empty array means this runs once on mount.

  return <div>Data fetched</div>;
};
Enter fullscreen mode Exit fullscreen mode

7. Context API

The Context API allows you to share values (like themes or user info) across your application without passing props through every level of the component tree. It is ideal for global state management.

Creating Context:

const MyContext = React.createContext();
Enter fullscreen mode Exit fullscreen mode

Using Context:

<MyContext.Provider value={/* some value */}>
  <ChildComponent />
</MyContext.Provider>
Enter fullscreen mode Exit fullscreen mode

8. Virtual DOM

The Virtual DOM is an in-memory representation of the real DOM. React uses it to optimize rendering by calculating the differences between the current and previous states of the DOM, updating only what has changed.


Getting Started

  1. Set Up Environment: You can start a new React project using Vite:
npm create vite@latest my-app -- --template react
cd my-app
npm run dev
Enter fullscreen mode Exit fullscreen mode
  1. Learn by Doing: The best way to learn React is by building projects. Start with simple applications and gradually increase complexity as you become more comfortable.

  2. Explore Resources: Check out the official React documentation and online tutorials to deepen your understanding.

Top comments (0)