Published on

🚀 Core React Concepts - Understanding Components, JSX, Props, State, and Lifecycle Methods

Authors

React is one of the most popular JavaScript libraries for building user interfaces, and understanding its core concepts is essential to becoming a React pro. In this post, we’ll break down the fundamental concepts that form the foundation of React: Components, JSX, Props, State, and Lifecycle Methods. Let’s dive in and get started! ✨


1️⃣ Components: The Building Blocks of React 🏗️

React applications are made up of components. Components are like small, reusable pieces of code that return UI elements to be rendered on the screen.

There are two types of components in React:

  • Functional Components: A simple, stateless component that is written as a JavaScript function.
  • Class Components: A more traditional component that extends from React.Component and can manage state and lifecycle methods.

Example of a Functional Component:

function Welcome() {
  return <h1>Hello, React!</h1>
}

Example of a Class Component:

class Welcome extends React.Component {
  render() {
    return <h1>Hello, React!</h1>
  }
}

2️⃣ JSX (JavaScript XML): Writing HTML in JavaScript ✍️

One of the most unique features of React is JSX, which lets you write HTML-like code within JavaScript. It makes your React components more readable and concise, allowing you to express the UI structure more naturally.

JSX Example:

function Welcome() {
  return (
    <div>
      <h1>Hello, React!</h1>
      <p>This is a paragraph.</p>
    </div>
  )
}

But remember, JSX is not HTML—it's syntactic sugar that gets transpiled to JavaScript. Under the hood, JSX is converted into React.createElement() calls.

Important things to know about JSX:

  • Self-closing tags: Like in HTML, but must be explicitly closed in JSX.

    <img src="image.jpg" alt="description" />
    
  • JSX Expressions: You can embed JavaScript expressions inside JSX using curly braces

const name = 'Alice'
const element = <h1>Hello, {name}!</h1>

3️⃣ Props: Passing Data to Components 📤

Props (short for properties) allow you to pass data from a parent component to a child component. Props are immutable, meaning they cannot be changed by the child component.

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>
}

function App() {
  return <Greeting name="Alice" />
}

Here, the name prop is passed to the Greeting component from the App component. This allows Greeting to dynamically render different values based on the passed prop.


4️⃣ State: Managing Data in Components 💾

State is an object that holds the data that can change over time within a component. Unlike props, which are passed from parent to child, state is managed within the component. When the state changes, React automatically re-renders the component.

Example of State in a Class Component:

class Counter extends React.Component {
  constructor(props) {
    super(props)
    this.state = { count: 0 }
  }

  increment = () => {
    this.setState({ count: this.state.count + 1 })
  }

  render() {
    return (
      <div>
        <h1>Count: {this.state.count}</h1>
        <button onClick={this.increment}>Increment</button>
      </div>
    )
  }
}

Example of State in a Functional Component (with Hooks):

With React Hooks, functional components can now manage state, making them more powerful and concise!

import { useState } from 'react'

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

  const increment = () => setCount(count + 1)

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={increment}>Increment</button>
    </div>
  )
}

5️⃣ Lifecycle Methods: Controlling Component Lifecycle ⏳

Lifecycle methods are special methods that get called at different stages of a component’s life—when it’s created, updated, and destroyed. They are mainly used in class components, but with React Hooks, you can manage component lifecycle in functional components as well.

Common Lifecycle Methods (Class Components):

  • componentDidMount(): Runs once after the component mounts (ideal for fetching data).
  • componentDidUpdate(): Runs after a component updates (useful for reacting to state or prop changes).
  • componentWillUnmount(): Runs before the component is destroyed (great for cleaning up resources).

Example of componentDidMount:

class MyComponent extends React.Component {
  componentDidMount() {
    console.log("Component mounted!");
  }

  render() {
    return <h1>Hello, React!</h1>;
  }
}

Managing Lifecycle in Functional Components with useEffect:

The useEffect hook allows you to handle side effects like data fetching, subscriptions, and manual DOM changes in functional components.

import { useEffect } from 'react'

function MyComponent() {
  useEffect(() => {
    console.log('Component mounted!')
  }, []) // Empty array means it runs once after the component mounts

  return <h1>Hello, React!</h1>
}

Conclusion: 🎉

These core React concepts forms the backbone of React applications and are essential to understanding how to build dynamic and interactive user interfaces.

  • Components are the heart of any React app.
  • JSX makes writing components more intuitive and readable.
  • Props allow you to pass data between components.
  • State enables components to manage dynamic data.
  • Lifecycle Methods let you hook into the component’s life stages for tasks like data fetching or cleanup.
  • With these building blocks in hand, you’re well on your way to mastering React! 💪

🌐 Want to learn more about React? Stay tuned for more tutorials, tips, and tricks. Don’t forget to subscribe to our blog for the latest updates! 📬