Hooks

React Hooks

2022/06/30 (增加連結)

簡介

Hooks API

useState

const timer = setInterval(() => {

setTime((time) => time + 1);

}, 1000);

useEffect

useContext (Context)

useReducer

useCallback

useMemo

useRef

useImperativeHandle

useLayoutEffect

useDebugValue

useId (React 18)

useTransition (React 18)

useEvent (future)

custom hooks

Breakpoint

Fetch

Tips

async

createReducer

Design patterns

Fetch Data

Form

Global State Management

Map

Migration from class components

class NameOfComponent extends Component {

to

function NameOfComponent(props){

    • remove the constructor

    • remove the render() method, keep the return

    • add const before all methods

    • remove this.state throughout the component

    • remove all references to ‘this’ throughout the component

    • Set initial state with useState()

    • change this.setState() … instead, call the function that you named in the previous step to update the state…

    • replace compentDidMount with useEffect

    • replace componentDidUpdate with useEffect

Performance

Redux

Router

Suspense

  • When to use React Suspense vs React Hooks

    • React Suspense is to a Monad as Hooks are to Applicative Notation

    • Effectful.JS is a transpiler for embedding effectful into JavaScipt. It supports both Monadic and Applicative targets. It greatly simplifies programs in the designing, implementing, testing, and maintaining stages.

  • Hooks and Suspense- React’s Class Warfare

    • “Suspense” gives React developers the ability to suspend the rendering of a component while it’s waiting for the resolution of other a-synchronous operations.

    • “Concurrent Rendering,” similarly with “Suspense,” can pause expensive rendering processes to make high priority DOM updates first. It can be used in combination with “Suspense” to improve user experience by “skipping unnecessary loading states on fast connections.”

    • “Hooks” allow function components to use state and lifecycle features.

examples