React - The Complete Guide

Notes based on Udemy Course React - The Complete Guide (incl Hooks, React Router, Redux)

1. Getting Started

Writing Our First React Code

function Person(props) {
  return (
    <div className="person">
      <h1>{props.name}</h1>
      <p>Your Age: {props.age}</p>
    </div>
  );
}

ReactDOM.render(<Person name="John" age="26" />, document.querySelector('#p1'));

ReactDOM.render(<Person name="Sam" age="28" />, document.querySelector('#p2'));
function Person(props) {
  return (
    <div className="person">
      <h1>{props.name}</h1>
      <p>Your Age: {props.age}</p>
    </div>
  );
}

var app = (
  <div>
    <Person name="John" age="26" />
    <Person name="Sam" age="28" />
  </div>
);

ReactDOM.render(app, document.querySelector('#app'));

Why Should we Choose React

  1. UI State becomes difficult to handle with Vanilla Javascript
  2. Focus on Business Logic, not on preventing your App from exploding
  3. Plus: Framework Creators probably write better Code
  4. Huge Ecosystem, Active Community, High Performance

React Alternatives

Understanding Single Page Applications and Multi Page Applications

Two Kinds of Applications

Single Page Applications Multi Page Applications
Only ONE HTML Page, Content is (re)rendered on Client Multiple HTML Pages, Content is rendered on Server
Root react component, and other child components Typical HTML/CSS/JS, with maybe some React widgets
Typically only ONE ReactDOM.render() call One ReactDOM.render() call per "widget"

Course Outline

  1. Getting Started
  2. The Basics
  3. Debugging
  4. Styling Components
  5. Components Deep Dive
  6. HTTP Requests
  7. Routing
  8. Forms & Validation
  9. Redux
  10. Authentication
  11. Testing Introduction
  12. Deployment
  13. Bonus (Animations, Next Steps, Webpack)

2. Refreshing Next Generation Javascript (Optional)

3. Understanding the Base Features and Syntax

The Build Workflow

Using Create React App

npm i -g create-react-app

Understanding the Folder Structure

Understanding Component Basics

Understanding JSX

This code

return React.createElement(
  'div',
  {className: "App"},
  React.createElement('h1', null, 'Does this work now?')
);

is equivilant to this code:

return (
  <div className="App">
    <h1>Hi, I'm a React App</h1>
  </div>
);

JSX Restrictions

Creating a Functional Component

import React from 'react';

const person = () => {
  return <p>I'm a Person!</p>;
};

export default person;

In App.js: <Person />

Working with Components and Re-Using Them

Outputting Dynamic Content

// Person.js
import React from 'react';

const person = () => {
  return (
    <p>I'm a Person and I am {Math.floor(Math.random() * 30)} years old!</p>
  );
};

export default person;

Working with Props

In App.js:

<Person name="John" age="26" />
<Person name="Max" age="28" >My Hobbies: Racing</Person>
<Person name="Sam" age="23" />

in Person.js: I'm a {props.name} and I am {props.age} years old!

Understanding the children prop

Understanding and Using State

class App extends Component {
  state = {
    persons: [
      { name: 'John', age: 26 },
      { name: 'Max', age: 28 },
      { name: 'Same', age: 23 },
    ],
  };

  render() {
    return (
      <div className='App'>
        <h1>Hi, I'm a React App</h1>
        <p>This is really working!</p>
        <button>Switch Name</button>
        <Person
          name={this.state.persons[0].name}
          age={this.state.persons[0].age}
        />
        <Person
          name={this.state.persons[1].name}
          age={this.state.persons[1].age}
        >
          My Hobbies: Racing
        </Person>
        <Person
          name={this.state.persons[2].name}
          age={this.state.persons[2].age}
        />
      </div>
    );
  }
}

Handling Events with Methods

switchNameHandler = () => { console.log('Was clicked!'); };

<button onClick={this.switchNameHandler}>Switch Name</button>

Manipulating the State

switchNameHandler = () => {
  // console.log('Was clicked!');
  // DON'T DO THIS: this.state.persons[0].name = 'Jonathonas'
  this.setState({persons: [
    { name: 'Jonathonas', age: 26 },
    { name: 'Maximilian', age: 28 },
    { name: 'Sam', age: 23 },
  ]})
};

Using the useState() Hook for State Manipulation

Stateless vs Stateful Components

Passing Method References Between Components

Adding Two Way Binding

nameChangedHandler = (event) => {
  this.setState( {
    persons: [
      { name: 'Max', age: 28 },
      { name: event.target.value, age: 29 },
      { name: 'Stephanie', age: 26 }
    ]
  } )
}
// ...
<!-- in render(): -->
  <Person changed={this.nameChangedHandler} />
<!-- in Person component: -->
<input type='text' onChange={props.changed} />

Adding Styling with Stylesheets

.Person {
  width: 60%;
  margin: 16px auto;
  border: 1px solid #eee;
  box-shadow: 0 2px 3px #ccc;
  padding: 16px;
  text-align: center;
}

Working with Inline Styles

  render() {
    const style = {
      backgroundColor: 'white',
      font: 'inherit',
      border: '1px solid blue',
      padding: '8px',
      cursor: 'pointer',
    };
    return (
      // ...
      <button style={style} onClick={/* ... */}>
      // ...
    )
  }

Resources

4. Working with Lists and Conditionals

Rendering Content Conditionally

Handling Dynamic Content "The Javascript Way"

Outputting Lists

Lists and State

Updating State Immutably

Lists and Keys

Flexible Lists

5. Styling React Components and Elements

6. Debugging React Apps