React is a popular open-source JavaScript library developed by Facebook for building user interfaces, particularly single-page applications where data changes over time. It allows developers to create large web applications that can update and render efficiently in response to data changes.
React is not a framework but a library focused on the view layer of web applications. It uses components as the basic building blocks for UIs, which makes it easier to reason about your application and reuse code.
Before diving into React, ensure you have Node.js installed on your machine. You can download it from nodejs.org.
You can create a new React application using Vite, which is a modern build tool that offers a faster and more efficient development experience.
npm create vite@latest my-first-react-app --template react
cd my-first-react-app
npm install
npm run dev
This command will set up a new project directory called my-first-react-app, install all necessary dependencies, and start a development server. You can then open your browser to http://localhost:3000 to see the default React app running.
Components are the core concept in React. They are reusable pieces of code that return HTML-like JSX elements.
Functional components are simple JavaScript functions that return JSX.
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
Class components extend the React.Component class and must include a render() method that returns JSX.
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Props: Short for properties, props are read-only inputs to components. They allow you to pass data from a parent component to its children.
function App() {
return <Welcome name="John" />;
}
State: State is used to manage dynamic data within a component. It can change over time and trigger re-renders.
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>
);
}
JSX is a syntax extension for JavaScript that looks similar to HTML. It allows you to write elements in a more readable way.
const element = <h1>Hello, world!</h1>;
You can embed expressions within JSX using curly braces {}.
function formatName(user) {
return user.firstName + ' ' + user.lastName;
}
const user = { firstName: 'John', lastName: 'Doe' };
const element = (
<h1>
Hello, {formatName(user)}!
</h1>
);
JSX compiles to React.createElement() calls. This means you can use JSX anywhere you would use a JavaScript expression.
function getGreeting(user) {
if (user) {
return <h1>Hello, {formatName(user)}!</h1>;
}
return <h1>Hello, Stranger.</h1>;
}
Keep Components Pure: Avoid modifying the props or state of a component directly. Use setState for state changes and pass down callbacks to child components if you need to modify parent state.
Lift State Up: When multiple components need to access the same data, lift the shared state up to their closest common ancestor.
Use Functional Components with Hooks: Modern React development often favors functional components over class components due to the simplicity and power of hooks like useState and useEffect.
React.js is a powerful library for building dynamic user interfaces. By understanding its core concepts such as components, props, state, and JSX, you can start building complex applications with ease. As you progress, explore more advanced topics like hooks, context, and routing to enhance your React skills.
In the next section of this course, we will dive deeper into functional components and hooks, providing a comprehensive understanding of how to manage state and side effects in modern React applications.