-
-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decouple hooks from Element/BuildContext #144
Comments
What is the use-case? |
I will drop into this conversation, since I think I have a interesting use-case, expanding beyond hooks' initial scope (sharing stateful logic between widgets by composition). We are experimenting (and successfully applied to some simple projects) with an experimental architecture that uses hooks as a main state management solution. Snippet below demonstrates it's basic principle (classic "counter" example): class ScreenState {
final int value;
final Function() onButtonPressed;
}
ScreenState useScreenState() {
final counterState = useState(0);
return ScreenState(value: counterState.value, onButtonPressed: () => counterState.value++);
}
class Screen extends HookWidget {
@override
Widget build(BuildContext context) {
final state = useScreenState();
return ScreenView(state);
}
} You can further enhance this approach with This hook-based state composition expanded to the level of an entire app seems to produce quite elegant and maintainable (easy to refactor) code. Particularly, effects allow for intuitive, selective "listening" to changes in providers above, without the need to use streams. Cool thing is this approach does not prevent you from more stream-based style ( How does this connect to the original issue? There's one case, where this approach is a pain in the ass. Consider a global state which maintains connection to some kind of network stream (e.g. WebSockets, SSE), and exposes This could be resolved from decoupling hooks from flutter widget rebuilds, in a way similar to how
|
For exactly the background issue you have, we have created a pump that still pumps frames if the app is in background. |
Can you share details of your solution? This seems like a nice workaround. |
For anybody still interested in a solution - some time ago we created our own implementation of hooks: @rrousselGit if you think implementing similar capabilities to flutter_hooks might be valuable, I'll be happy to discuss & help. |
If flutter_hooks could abstract over its dependencies to the flutter framework for scheduling, we could drop in a different scheduling engine and use the hooks also outside of Widget trees.
The text was updated successfully, but these errors were encountered: