Introduction to React
React is a popular JavaScript library used for building user interfaces and can be used for developing complex, interactive web and mobile applications. In this article, we will cover the basics of React and provide a step-by-step guide on how to get started with React in 2026.
Setting Up the Development Environment
To start with React, you need to have Node.js installed on your machine. You can download it from the official Node.js website. Once you have Node.js installed, you can install create-react-app by running the following command in your terminal:
npx create-react-app my-app
This will create a new React project in a folder named my-app. You can then navigate into the project folder and start the development server by running:
cd my-app
npm start
This will start the development server and open the application in your default web browser.
Understanding JSX
JSX is a syntax extension for JavaScript that allows you to write HTML-like code in your JavaScript files. Here is an example of JSX:
import React from 'react';
function HelloWorld() {
return <h1>Hello, World!</h1>;
}
In this example, we define a React component named HelloWorld that returns an <h1> element with the text "Hello, World!".
Components
In React, components are the building blocks of your application. There are two types of components: functional components and class components.
Functional Components
Functional components are simple functions that return JSX. Here is an example of a functional component:
import React from 'react';
function Counter() {
const [count, setCount] = React.useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
In this example, we define a functional component named Counter that uses the useState hook to store the count in the component's state.
Class Components
Class components are classes that extend the React.Component class. Here is an example of a class component:
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Increment
</button>
</div>
);
}
}
In this example, we define a class component named Counter that uses the constructor method to initialize the component's state.
State and Props
State and props are two important concepts in React.
- State: The state of a component is an object that stores the component's data. The state can change over time, and when it does, the component will re-render.
- Props: Props are short for "properties" and are used to pass data from a parent component to a child component.
Here is an example of using state and props:
import React, { useState } from 'react';
function Parent() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
<Child count={count} />
</div>
);
}
function Child(props) {
return <p>Child count: {props.count}</p>;
}
In this example, we define a parent component that uses the useState hook to store the count in the component's state. We then pass the count as a prop to the child component.
Event Handling
Event handling is an important concept in React. Here is an example of event handling:
import React from 'react';
function Button() {
function handleClick() {
console.log('Button clicked!');
}
return <button onClick={handleClick}>Click me!</button>;
}
In this example, we define a button component that uses the onClick event handler to log a message to the console when the button is clicked.
Hooks
Hooks are a new way to use state and other React features in functional components. Here are some common hooks:
- useState: Used to store state in a functional component.
- useEffect: Used to handle side effects, such as fetching data from an API.
- useContext: Used to access context (shared state) in a functional component.
Here is an example of using the useState hook:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
In this example, we define a counter component that uses the useState hook to store the count in the component's state.
Best Practices
Here are some best practices to keep in mind when building React applications:
- Keep components small and focused: Each component should have a single responsibility and should be small and focused.
- Use functional components: Functional components are easier to reason about and are generally preferred over class components.
- Use hooks: Hooks are a powerful way to use state and other React features in functional components.
- Use a consistent coding style: Use a consistent coding style throughout your
☕ Factual
Top comments (0)