CJY0208 / react-router-cache-route

Route with cache for react-router V5 like <keep-alive /> in Vue
https://www.npmjs.com/package/react-router-cache-route
MIT License
1.16k stars 113 forks source link

Being able to add multiple handlers for life cycle methods #98

Closed ali-garajian closed 3 years ago

ali-garajian commented 3 years ago

Right now with the methods "didCache" and "didRecover", one could only add one callback for each life cycle. In my case, I'm using these methods in multiple custom hooks, and each of them needs to add its own callback. But since that's not possible, I have to lift the logic up to the their parent and expose callbacks from my hooks and call them all at one place, which kinda leads to smelly and duplicate code! It would have been great if something like "didCache.addListener(() => {})" was possbile.

CJY0208 commented 3 years ago

v1.11.0 lifecycle hooks supports multiple handlers

import { useDidCache, useDidRecover } from 'react-router-cache-route'

export default function List() {

  useDidCache(() => {
    console.log('List cached 1')
  })

  // support multiple effect
  useDidCache(() => {
    console.log('List cached 2')
  })

  useDidRecover(() => {
    console.log('List recovered')
  })

  return (
    // ...
  )
}