"A concise introduction to React, covering essential concepts with simple examples."
By Samir Niroula
28 October 2024React is a JavaScript library for building interactive user interfaces. It simplifies the process of creating dynamic web applications by using reusable components.
To start a React project, you can use Create React App for a basic setup.
npx create-react-app my-app
cd my-app
npm start
This creates a development environment with all necessary dependencies.
JSX allows us to write HTML-like syntax in JavaScript files, making it easier to create UI components.
const element = <h1>Hello, React!</h1>;
JSX expressions are compiled to JavaScript, making them compatible with the browser.
Components are reusable UI blocks in React. They can be functional or class-based.
Functional Component Example:
function Greeting() {
return <h1>Welcome to React!</h1>;
}
Class Component Example:
class Greeting extends React.Component {
render() {
return <h1>Welcome to React!</h1>;
}
}
Props (properties) allow you to pass data into components.
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
<Welcome name="Samir" />;
Here, name is a prop, allowing you to pass dynamic data into Welcome.
State is used to manage data within a component, making components interactive.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
useState creates a state variable (count) and a function to update it (setCount).
React handles events similarly to HTML, but with camelCase syntax and functions.
function ClickButton() {
const handleClick = () => alert("Button clicked!");
return <button onClick={handleClick}>Click Me</button>;
}
Here, onClick triggers handleClick when the button is clicked.
Use conditional rendering to display different content based on conditions.
function Message({ isLoggedIn }) {
return isLoggedIn ? <h1>Welcome Back!</h1> : <h1>Please Sign In</h1>;
}
The isLoggedIn prop controls which message is displayed.
Use the map method to render lists in React, and add a unique key prop to each item.
const fruits = ["Apple", "Banana", "Cherry"];
function FruitList() {
return (
<ul>
{fruits.map((fruit) => (
<li key={fruit}>{fruit}</li>
))}
</ul>
);
}
The key prop helps React identify and manage each list item.
In class components, lifecycle methods allow you to control component behavior at different stages.
class Timer extends React.Component {
componentDidMount() {
console.log("Component mounted!");
}
render() {
return <p>Timer Component</p>;
}
}
componentDidMount runs after the component is added to the DOM.
Hooks enable you to use state and lifecycle features in functional components.
import { useEffect } from "react";
function FetchData() {
useEffect(() => {
console.log("Component mounted or updated!");
}, []);
return <p>Data Component</p>;
}
useEffect runs on mount or when specified dependencies change.
Forms in React handle inputs through controlled components.
function Form() {
const [name, setName] = useState("");
return (
<form>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<p>Your name is: {name}</p>
</form>
);
}
Here, the input’s value is linked to name state, making it controlled.
React simplifies building interactive UIs with reusable components, state management, and hooks. Start experimenting, and you’ll quickly see the power of React in creating dynamic applications. Happy coding!