0%

23. ReactContext

React Context

: pass through App -> Sidebar + ( Dashboard -> AddComment)

  1. Provider: Owner
  2. Consumer(s)
    • share the parent
    • a subtree of the provider
  • argument = null => inaccessible to the provider

  • value = {42} => give the value to the CurrentUserContext.Provider, give access

  • { value => } => give CurrentUserContext.Provider a Function

–> Modify

Hooks

  1. Classes
  2. Reuse stateful logic is hard
  3. Logic spread into lifecycle methods

Only call hooks at/from…

  • the top level
  • function components or custom hooks

Function Components

  • function name with ‘use’
  • useState()

setCounter(counter+1) => (update): counter = counter+1

  • useEffect()
  • useEffect without [] (initializing) => call setInterval exponentially (setInterval itself calls the function exponentially, it’s like ex*ex)
  • useContext()