async_redux icon indicating copy to clipboard operation
async_redux copied to clipboard

Provide official useSelector and useDispatch hooks (for flutter_hooks)

Open bugeats opened this issue 5 years ago • 2 comments

I'm a Flutter noob trying to get the nice state management stack that I'm accustomed to from react-redux. The combination of useSelector and useDispatch hooks is the cleanest and most concise state management strategy I've yet encountered, especially combined with reselect.

The flutter_hooks package seems to provide similar react hooks functionality, but it appears that the dirty-checking model is totally different. For example useState returns a ValueNotifier wrapped value instead of just the actual value. I'm afraid it's currently beyond my Flutter experience to understand why that is and how to use it to get what I want.

So far I have this:

// seems to work ok, probably not done correctly
useDispatch() {
  final BuildContext context = useContext();

  final redux.Store<AppState> store =
      redux.StoreProvider.of<AppState>(context, false);

  return store.dispatch;
}

And this:

// Does NOT re-render the widget when store state changes
useSelector(Function selectorFn) {
  final BuildContext context = useContext();
  final AppState state = redux.StoreProvider.state<AppState>(context);

  return selectorFn(state);
}

It seems that @marcglasberg and @rrousselGit have collaborated successfully in the past. Maybe you guys can help me see this dream accomplished? Let me know if I should move this ticket to a different repo.

bugeats avatar May 05 '20 17:05 bugeats

I'm also afraid that simply understanding what you ask is currently beyond my Flutter experience... I have no experience at all with flutter_hooks. Sorry.

marcglasberg avatar May 05 '20 17:05 marcglasberg

In my opinion, React Hooks are the best thing that's ever happened to React and reactive GUIs since functional components. It may be worth the time to take a look at what flutter_hooks is trying to do.

So far I'm getting the impression that Flutter/Dart is so heavily object-oriented that these sorts of simple functional approaches tend not to arise naturally.

bugeats avatar May 05 '20 17:05 bugeats