This article records usagereact hookPoints to note in the process and how they differ from class components

useState

const [state, setState] = useState(initialState);

  1. SetState usage
  • Receives a new state value
  • Receives a function that updates the state value with the previous state value
  1. ⚠ ️ pay attention to the point
  • SetState is not synchronous, it does not change the value of state immediately
  • If the updated value is exactly the same as the current state, subsequent rerendering will be skipped entirely (reference types often change directly on the old value, causing the page not to be rerendered)
  • React uses the object. is comparison algorithm to compare states