Skip to content

Latest commit

 

History

History
60 lines (53 loc) · 2.59 KB

File metadata and controls

60 lines (53 loc) · 2.59 KB

Thinking In React

Section Source

Steps for Designing In React:

  1. Develop a Component Hierarchy
    • draw boxes around each component and sub-component and assign names
    • use the single responsibility principle - each component should only do one (1) thing, if a component starts handling too much it should be broken down into sub-components
      • UI and data models tend to both follow the same information architecture
      • each component should match one piece of your data model
  2. Build a Static Version
    • component hierarchy uses data model and render the UI
    • no interactivity
      • use only props, do not create any state
      • render() is the only method
    • results in library of reusable components
  3. Identify MVP of UI State
    • need minimal set of mutable state required to have functioning app
    • DRY - Don't Repeat Yourself
    • determining state:
    1. passed via props? ❌ state
    2. unchanged over time? ❌ state
    3. computable by other state or props? ❌ state
  4. Identify Where the State Lives
    • determine which component mutates or owns each state
    • remember React is one-way data flow
    • foreach(state)
      • identify each component that renders something based on the state
      • find common parent component
      • state should live in common parent or even higher in hiearchy
      • if common parent doesn't exist, make one
  5. Inverse Data Flow
    • use callbacks to update state where required from child components

Higher-Order Functions

Section Source

Higher-Order Functions - functions that operate on other functions by using them as arguments or returning them.

Greater than function:

function greaterThan(n) {
  return m => m > n;
}
let greaterThan10 = greaterThan(10);
console.log(greaterThan10(11));
// → true
  • takes in a number n
  • returns an arrow function that takes an argument m and compares it to n using greater than

Higher-Order Functions are great for handling data

  • built in methods like filter() utilize an array and a built in test to test against that array.
  • map() functions by applying a function to all of the elements arguement array
    • the application of the this function to transform data and push into a new array makes map a higher-order function
  • reduce() builds a value by repeatedly combining a single element from an array with the current value
    • it does this with array, combination function, and start value parameters

Things I want to know more about:

  • How the developer of React built it