### React Basics Course Outline

 

 

 

1. **Introduction to React**

 


    - 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`.
```jsx
// Functional Component
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}
// Class Component
class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}
```
**Creating and Exporting Components:**
Create a new file for your component, define the component, and then export it.
```jsx
// Welcome.js
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}
export default Welcome;
```
**Importing and Using Components:**
Import the component into another file and use it within JSX.
```jsx
import Welcome from './Welcome';
function App() {
  return (
    <div>
      <Welcome name="Sara" />
      <Welcome name="Cahal" />
      <Welcome name="Edite" />
    </div>
  );
}
```
**Component Nesting:**
Components can be nested within other components to build complex UIs.
```jsx
function App() {
  return (
    <div>
      <Welcome name="Sara" />
      <Welcome name="Cahal" />
      <Welcome name="Edite" />
    </div>
  );
}
```

 

 

 

 

 

 

 

 

#### 5. Props and State
 

**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');
}
<button onClick={handleClick}>Click me</button>;
```
**Binding Event Handlers:**
In class components, you may need to bind event handlers to `this`.
```jsx
class Toggle extends React.Component {
  constructor(props) {
    super(props);
    this.state = { isOn: true };
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick() {
    this.setState(state => ({
      isOn: !state.isOn
    }));
  }
  render() {
    return (
      <button onClick={this.handleClick}>
        {this.state.isOn ? 'ON' : 'OFF'}
      </button>
    );
  }
}
```
**Passing Arguments to Event Handlers:**
You can pass additional arguments to event handlers using arrow functions or the `bind` method.
```jsx
function handleClick(id) {
  console.log('Button clicked with id:', id);
}
<button onClick={() => handleClick(1)}>Click me</button>;
```

 

 

 

 

 

 

 

#### 7. Lifecycle Methods

**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.
```jsx
class Clock extends React.Component {
  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }
  componentWillUnmount() {
    clearInterval(this.timerID);
  }
  tick() {
    this.setState({
      date: new Date()
    });
  }
  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this
.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}
```
**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());
  useEffect(() => {
    const timerID = setInterval(() => setDate(new Date()), 1000);
    return () => clearInterval(timerID);
  }, []);
  return <h1>It is {date.toLocaleTimeString()}.</h1>;
}
```

 

 

 

 

 

 

#### 8. Conditional Rendering

**Using If-Else Statements:**
You can use JavaScript if-else statements to conditionally render components.
```jsx
function Greeting(props) {
  const isLoggedIn = props.isLoggedIn;
  if (isLoggedIn) {
    return <h1>Welcome back!</h1>;
  }
  return <h1>Please sign up.</h1>;
}
```
**Ternary Operators:**
Ternary operators provide a shorthand way to perform conditional rendering.
```jsx
function Greeting(props) {
  return props.isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>;
}
```
**Short-Circuit Evaluation:**
Short-circuit evaluation allows you to render a component based on a condition without using an if statement.
```jsx
function Mailbox(props) {
  const unreadMessages = props.unreadMessages;
  return (
    <div>
      <h1>Hello!</h1>
      {unreadMessages.length > 0 &&
        <h2>You have {unreadMessages.length} unread messages.</h2>
      }
    </div>
  );
}
```

 

 

 

 

 

 

 

#### 9. Lists and Keys
 

**Rendering Lists of Data:**
Use the `map` function to render lists of data.
```jsx
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
  <li key={number.toString()}>
    {number}
  </li>
);
```
**Using Keys in Lists:**
Keys help React identify which items have changed, are added, or are removed.
```jsx
function NumberList(props) {
  const numbers = props.numbers;
  const listItems = numbers.map((number) =>
    <li key={number.toString()}>
      {number}
    </li>
  );
  return (
    <ul>{listItems}</ul>
  );
}
```

 

 

 

 

 

 

#### 10. Forms and Controlled Components
 

**Handling Form Inputs:**
React handles form inputs through controlled components, where the form data is handled by the component's state.
```jsx
class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }
  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="text" value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}
```
**Controlled vs. Uncontrolled Components:**
Controlled components rely on React to manage form data, while uncontrolled components rely on the DOM.
**Handling Form Submission:**
Form submission in React is handled through event handling methods.

 

 

 

 

 

 

#### 11. Basic Hooks
 

**useState:**
The `useState` hook allows you to add state to functional components.
```jsx
import React, { useState } from 'react';
function Example() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}
```
**useEffect:**
The `useEffect` hook allows you to perform side effects in functional components.
```jsx
import React, { useState, useEffect } from 'react';
function 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>
  );
}
```
**useContext:**
The `useContext` hook allows you to use context within functional components.
```jsx
import React, { useContext } from 'react';
const ThemeContext = React.createContext('light');
function ThemedButton() {
  const theme = useContext(ThemeContext);
  return <button className={theme}>Button</button>;
}
```

 

 

 

 

 

#### 12. Conclusion and Next Steps
 

**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.