- What is React?
- Key features of React
- Why use React?
2. **Setting Up the Development Environment**
- Installing Node.js and npm
- Setting up a React project with Create React App
- Overview of the project structure
3. **JSX: JavaScript XML**
- What is JSX?
- Embedding expressions in JSX
- JSX attributes and children
- Rendering JSX
4. **Components**
- Functional vs. class components
- Creating and exporting components
- Importing and using components
- Component nesting
5. **Props and State**
- Understanding props
- Passing props to components
- Understanding state
- Managing state in class components
- The useState hook in functional components
6. **Event Handling**
- Handling events in React
- Binding event handlers
- Passing arguments to event handlers
7. **Lifecycle Methods**
- Overview of component lifecycle
- Common lifecycle methods in class components
- Using useEffect hook for lifecycle management in functional components
8. **Conditional Rendering**
- Using if-else statements
- Ternary operators
- Short-circuit evaluation
9. **Lists and Keys**
- Rendering lists of data
- Using keys in lists
10. **Forms and Controlled Components**
- Handling form inputs
- Controlled vs. uncontrolled components
- Handling form submission
11. **Basic Hooks**
- useState
- useEffect
- useContext
12. **Conclusion and Next Steps**
- Summary of key concepts
- Resources for further learning
#### 1. Introduction to React
**What is React?**
React is a JavaScript library for building user interfaces, developed by Facebook. It allows developers to create large web applications that can update and render efficiently in response to data changes.
**Key Features of React:**
- **Component-Based:** Build encapsulated components that manage their own state, then compose them to make complex UIs.
- **Declarative:** React makes it easy to design interactive UIs, by letting you describe how your UI should look at any given state.
- **Learn Once, Write Anywhere:** You can use React to create applications for the web, mobile with React Native, and even VR.
**Why Use React?**
React simplifies the process of building dynamic and interactive user interfaces, encourages reusable component design, and offers excellent performance due to its virtual DOM implementation.
#### 2. Setting Up the Development Environment
**Installing Node.js and npm:**
To use React, you need Node.js and npm (Node Package Manager). Download and install the latest version from the [official Node.js website](https://nodejs.org/).
**Setting Up a React Project with Create React App:**
Create React App is a command-line tool that sets up a new React project with a sensible default configuration. Run the following command in your terminal:
```bash
npx create-react-app my-app
cd my-app
npm start
```
This will create a new React application named "my-app" and start the development server.
**Overview of the Project Structure:**
The default project structure created by Create React App includes the following folders and files:
- `public`: Contains the public assets of the app, such as `index.html`.
- `src`: Contains the source code of the app, including components, styles, and tests.
#### 3. JSX: JavaScript XML
**What is JSX?**
JSX is a syntax extension for JavaScript that looks similar to XML or HTML. It is used with React to describe what the UI should look like.
**Embedding Expressions in JSX:**
You can embed any JavaScript expression within JSX by wrapping it in curly braces `{}`.
```jsx
const element = <h1>Hello, {name}!</h1>;
```
**JSX Attributes and Children:**
JSX elements can have attributes, and they can contain children elements.
```jsx
const element = <a href="https://reactjs.org">Click Here</a>;
```
**Rendering JSX:**
JSX needs to be transpiled into JavaScript. React uses Babel to convert JSX into `React.createElement` calls.
```jsx
const element = <h1>Hello, world!</h1>;
ReactDOM.render(element, document.getElementById('root'));
```
#### 4. Components
**Functional vs. Class Components:**
- **Functional Components:** Simplest way to create a component. It is a JavaScript function that returns JSX.
- **Class Components:** More feature-rich way to create a component. It is a JavaScript class that extends `React.Component`.
**Understanding Props:**
Props (short for "properties") are read-only attributes that are passed to components from their parent.
**Passing Props to Components:**
Props are passed to components similarly to HTML attributes.
```jsx
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
function App() {
return <Welcome name="Sara" />;
}
```
**Understanding State:**
State is a built-in object that allows components to create and manage their own data. State can change over time, and when it does, the component re-renders.
**Managing State in Class Components:**
State is initialized in the constructor and updated using `setState`.
```jsx
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = { date: new Date() };
}
render() {
return <h1>It is {this.state.date.toLocaleTimeString()}.</h1>;
}
}
```
**The useState Hook in Functional Components:**
The `useState` hook allows you to add state to functional components.
```jsx
import React, { useState } from 'react';
function Clock() {
const [date, setDate] = useState(new Date());
return <h1>It is {date.toLocaleTimeString()}.</h1>;
}
```
#### 6. Event Handling
**Handling Events in React:**
React handles events similarly to handling DOM events, but with some syntactic differences.
```jsx
function handleClick() {
console.log('Button clicked');
}
**Overview of Component Lifecycle:**
React components go through a lifecycle of mounting, updating, and unmounting.
**Common Lifecycle Methods in Class Components:**
- `componentDidMount`: Called after the component is rendered for the first time.
- `componentDidUpdate`: Called after the component updates.
- `componentWillUnmount`: Called before the component is removed from the DOM.
**Using useEffect Hook for Lifecycle Management in Functional Components:**
The `useEffect` hook allows you to perform side effects in functional components.
```jsx
import React, { useState, useEffect } from 'react';
function Clock() {
const [date, setDate] = useState(new Date());
**Summary of Key Concepts:**
This course has covered the basics of React, including components, state, props, event handling, lifecycle methods, conditional rendering, lists, forms, and basic hooks.
**Resources for Further Learning:**
- [React Official Documentation](https://reactjs.org/docs/getting-started.html)
- [React Tutorial: Learn React & Build an App](https://reactjs.org/tutorial/tutorial.html)
- [FreeCodeCamp React Curriculum](https://www.freecodecamp.org/learn/front-end-libraries/#react)
This concludes our basic course on React. With these fundamentals, you are well-equipped to start building your own React applications and dive deeper into more advanced topics as needed.