byte-fe / react-model

The next generation state management library for React
236 stars 23 forks source link

Improve useStore's performance on huge dataset #46

Open ArrayZoneYour opened 5 years ago

ArrayZoneYour commented 5 years ago

Every useStore create a setState now. The cost of communicator middleware will be expensive when the new state passed in setState is huge and the number of components(use useStore hooks) is big.

Enhance:

// before
const useStore = (modelName: string, depActions?: string[]) => {
  const [, setState] = useState(Global.State[modelName].state)
  // ...
}

// expected
const useStore = (modelName: string, depActions?: string[]) => {
  // If Context exist
  const { [modelName]: { setState } } = useContext(GlobalContext)
  // If not
  const [, setState] = useState(Global.State[modelName].state)
  // ...
}