I Love ReactJS

A Comprehensive Overview of Key React Concepts

React is a popular JavaScript library for building user interfaces, and it offers a wide range of features and concepts that developers need to understand to build efficient and maintainable applications. In this article, we will delve into some of the key React concepts, providing clear explanations and illustrative examples to help you grasp these fundamental concepts effectively.

1. JSX (JavaScript XML)

JSX allows you to write UI components using a syntax that closely resembles HTML. It provides a more readable and concise way to describe the UI components.

const element = <h1>Hello, World!</h1>; ReactDOM.render(element, document.getElementById('root'));

2. Components and Props

React components are the building blocks of a React application. They are reusable and encapsulate the UI logic. Props are used to pass data from parent to child components.

const Welcome = (props) => { return <h1>Hello, {props.name}</h1>; }; ReactDOM.render(<Welcome name="John" />, document.getElementById('root'));

3. State and Lifecycle

State allows React components to create and manage their own data. Lifecycle methods are special methods that execute at specific phases in a component's lifecycle.

class Clock extends React.Component { constructor(props) { super(props); this.state = { date: new Date() }; } componentDidMount() { this.timerID = setInterval(() => this.tick(), 1000); } componentWillUnmount() { clearInterval(this.timerID); } tick() { this.setState({ date: new Date() }); } render() { return <h2>It is {this.state.date.toLocaleTimeString()}.</h2>; } } ReactDOM.render(<Clock />, document.getElementById('root'));

4. Hooks

Hooks are a new addition in React 16.8 that allow you to use state and other React features without writing a class.

useState

import React, { useState } from 'react'; const Counter = () => { const [count, setCount] = useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>Click me</button> </div> ); };

useEffect

import React, { useState, useEffect } from 'react'; const Example = () => { const [count, setCount] = useState(0); useEffect(() => { document.title = `You clicked ${count} times`; }); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>Click me</button> </div> ); };

5. Conditional Rendering

Conditional rendering allows you to display different UI elements based on certain conditions.

const Greeting = (props) => { if (props.isLoggedIn) { return <h1>Welcome back!</h1>; } else { return <h1>Please sign up.</h1>; } }; ReactDOM.render(<Greeting isLoggedIn={false} />, document.getElementById('root'));

6. Lists and Keys

Lists are a fundamental part of React applications. The map() function is often used to render a list of items.

const numbers = [1, 2, 3, 4, 5]; const listItems = numbers.map((number) => <li key={number.toString()}>{number}</li>); ReactDOM.render(<ul>{listItems}</ul>, document.getElementById('root'));

7. Forms and Controlled Components

Forms in React are slightly different from traditional HTML forms, and they often use controlled components to manage form data.

class NameForm extends React.Component { constructor(props) { super(props); this.state = { value: '' }; } handleChange = (event) => { this.setState({ value: event.target.value }); }; handleSubmit = (event) => { alert('A name was submitted: ' + this.state.value); event.preventDefault(); }; render() { return ( <form onSubmit={this.handleSubmit}> <label> Name: <input type="t
Exit mobile version