📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • Build Workflow
  • Understanding JSX
  • JSX restrictions
  • JSX expressions
  • Component naming conventions
  • Components
  • Stateless vs. stateful components
  • props.children
  • State (and props)
  • useState Hook
  • Passing Method References Between Components
  • Styling Components
  1. Javascript
  2. React Complete Guide

React Basics

Build Workflow

Creating a build workflow matters because:

  • We want to ship optimized code (e.g. small file sizes)

  • We want to use ES6 features

  • We want to be productive (linting, CSS prefixes)

For a build workflow, we need the following:

  • Dependency management tool like npm or yarn

  • Bundler like Webpack that allows us to split code into multiple, modular files, which bundle into a couple of files in the end at build

  • Compiler like Babel + Presets that converts ES6 JS to ES5 or lower

    • Note: Babel is part of the Webpack configuration

  • Development server that allows local testing on machine

Pro tip: create-react-app is a package that handles all of this for us out of the box!

Understanding JSX

JSX is basically syntactic sugar that gets compiled down to nested React.createElement calls:

return (
  <div className="App">
    <h1>React App</h1>
  </div>
);

// is equivalent to...

return React.createElement(
  "div",
  { className: "App" },
  React.createElement(
    "h1",
    null,
    "React App"
  )
);

When React sees JSX, it basically creates nested elements using nested React.createElement calls.

Pro tip: That's why you have to write import React from "react"; at the top of your file!

JSX restrictions

  • There are special JSX attributes like className that aren't 100% identical to HTML attributes.

  • Components have to return 1 parent JSX element. (Note: Adjacent returned elements are possible but not recommended.)

JSX expressions

You can display the evaluation of a JavaScript expression inside your JSX using {}:

<p>1 + 1 = {1 + 1}</p>

Note: The {} only take JavaScript expressions. Nothing else.

Component naming conventions

It's best practice to capitalize your component names, so that they don't conflict with other JSX you render on the page. For example, Div is different from div.

Components

Stateless vs. stateful components

Functional and class components tend to get broken up into stateless and stateful components.

Stateless components have the following descriptions:

  • Dumb (they don't have logic)

  • Presentational (they just display stuff)

Stateful components are described as:

  • Smart (they have logic)

  • Containers (they contain information)

Best practice: You should almost always use stateless components because they're easier to manage. Minimize the number of stateful components to minimize over-complexity.

props.children

Your custom components can have content nested inside them:

<Component name="Dan">Hello!</Component>

To access "Hello!", I can just call props.children:

const Component = (props) => {
  return (
    <div>
      <p>My name is {props.name}!</p>
      <p>{props.children}</p>
    </div>
  );
};

Note: props.children doesn't have to be text. It can be more JSX!

State (and props)

props is useful when you want to get information from outside the component.

state is useful when you want to control information from inside the component.

Anytime you update props or state, React checks the DOM to see if information needs changing and makes the appropriate changes.

useState Hook

As of React 16.8, functional components have state using React hooks.

React hooks are functions like useState that add extra functionality to your functional components.

Here's the code pattern:

import React, { useState } from "react";

const app = () => {
  // useState makes React aware of initial state, returning an array with:
  // 1. current state
  // 2. setState method
  const [counterState, setCounterState] = useState({
    counter: 1
  });

  // Here's an event callback function just like normal!
  const handleClick = () => {
    // setState works as normal
    setCounterState({
      counter: counterState.counter + 1
    });
  };

  // Referencing state works normally too!
  return (
    <div>
      <p>{counterState.counter}</p>
      <button onClick={handleClick}>Increase initial state</button>
    </div>
  );
};

Major difference: useState doesn't carry over untouched state when using setCounterState. You must manually bring the state over.

const [counterState, setCounterState] = useState({
  counter: 1,
  otherState: "hello"
});

setCounterState({
  counter: counterState.counter + 1,
  otherState: counterState.otherState // manual!
});

Best practice: Instead of updating state all with one method, it's better to useState for each piece of data you want to keep track of. This is known as state slicing.

Note: useState can take any piece of data, not just objects!

const [counterState, setCounterState] = useState({
  counter: 1
});

const [otherState, setOtherState] = useState("some other value");

Pro tip: Technically, you don't need to use class-based components at all anymore. However, class-based components are still considered the standard for state management.

Passing Method References Between Components

There are 2 known ways to pass a method to another component

Anonymous function

<Component onClick={() => this.handleClick("newValue")} />

bind method

<Component onClick={this.handleClick.bind(this, "newValue")} />

Pro tip: The anonymous function pattern is actually inefficient. It sometimes triggers unnecessary re-renders. bind is better.

Styling Components

There are 2 ways to styles your components:

  1. Stylesheet

  • Add className to component

  • Style class using .css stylesheet

  • Import .css file into component

Note: Import basically makes Webpack aware of the .css file, which will include a link to it in the HTML

// Component
<Component className="component" />

// Stylesheet
.component {
  margin: 0 auto;
}

// Import
import "./component.css";
  1. Inline styling

  • Create a JS style object variable

  • Pass style object into style JSX attribute of component

Note: Inline styles don't have access to all CSS features (e.g. :hover).

render() {
  const style = {
    backgroundColor: "white"
  };

  return (
    <Component style={style} />
  );
};
PreviousWhat Is ReactNextRendering Lists And Conditionals

Last updated 3 years ago