markdalgleish / redial

Universal data fetching and route lifecycle management for React etc.
1.1k stars 42 forks source link

Easing migration from react-fetcher? #23

Open levity opened 8 years ago

levity commented 8 years ago

I have an app that uses the prefetch decorator, from redial's previous life as react-fetcher, thirty-plus times. I'd like to upgrade to redial for the ability to attach arbitrary lifecycle hooks even to stateless components, but I'm not looking forward to what promises to be a tedious migration... Has anyone written code to ease the migration or to continue supporting the old prefetch and defer decorators with the new version?

walshie4 commented 7 years ago

The migration is fairly simple, basically all you need to do is to switch the names. The main difference is rather than use defer and prefetch directly their argument just gets passed to either the fetch or defer key in the provideHooks object.

very light examples:

//before
prefetch( /* function that dispatches goes here */);
defer( /* function that dispatches goes here */);

//after
provideHooks({ 
  fetch: /* function that dispatches goes here */,
  defer: /* function that dispatches goes here */,
});

Server side / control code is also fairly simple.

//before
getDeferredData(components, obj);
//after
trigger('defer', components, obj);