Comments (5)
Hi, @wiseaidev
At the heart of MobX are observables. The reactivity occurs when there is a reaction that depends on an observable. When the observables change, the associated reactions will re-trigger automatically. Yes, automatically, without any manual wiring. The way you link a reaction to an observable is by using it (reading its value) inside the tracking function.
https://mobx.netlify.app/guides/when-does-mobx-react#rules-of-reactivity
https://mobx.netlify.app/api/observers#reactionbuilder-widget
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: ReactionBuilder(
builder: (context) {
return reaction(
(_) => context.read<AppState>().currentPage , (v) => print('New value: $v')
},
);
},
child: Observer(
name: "CurrentScreen",
builder: (context) {
switch (context.read<AppState>().currentPage) {
case AppPage.splash:
return const MySplashPage(title: "Splash Page");
case AppPage.home:
return const MySplashPage(title: "Home Page");
case AppPage.login:
return const Placeholder();
}
},
),
),
);
}
}
https://mobx.netlify.app/api/reaction#autorun
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: ReactionBuilder(
builder: (context) {
return autorun(
(_) {
print("A reaction has been triggered! ${context.read<AppState>().currentPage}");
},
);
},
child: Observer(
name: "CurrentScreen",
builder: (context) {
switch (context.read<AppState>().currentPage) {
case AppPage.splash:
return const MySplashPage(title: "Splash Page");
case AppPage.home:
return const MySplashPage(title: "Home Page");
case AppPage.login:
return const Placeholder();
}
},
),
),
);
}
}
Installing build/app/outputs/flutter-apk/app-debug.apk...
Debug service listening on ws://127.0.0.1:44217/gaOdK7mROOQ=/ws
Syncing files to device sdk gphone64 x86 64...
I/x_autorun_issue( 7449): Compiler allocated 4533KB to compile void android.view.ViewRootImpl.performTraversals()
I/flutter ( 7449): A reaction has been triggered! AppPage.splash
E/SurfaceSyncer( 7449): Failed to find sync for id=0
W/Parcel ( 7449): Expecting binder but got null!
I/flutter ( 7449): Am action has been fired!
I/flutter ( 7449): A reaction has been triggered! AppPage.home
D/EGL_emulation( 7449): app_time_stats: avg=14432.15ms min=18.67ms max=28845.63ms count=2
from mobx.dart.
Hey @amondnet,
Thanks for the response. I will try your suggestion and get back in an hour or so cause I am AFK at the moment(using gh mobile).
from mobx.dart.
@wiseaidev Please reopen if you have more questions.
from mobx.dart.
Hey @amondnet,
I have just tried both examples, but the problem still exists.
I think you are using an older version of flutter. My specs:
Therefore, I am going to reopen the issue (Oh wait! I can't do it :-(. I need karma or something).
from mobx.dart.
Hey @amondnet,
After lil speculation, I found that the problem exists on SDK > 2.18
(but probably on SDK >2.17
as well):
I tried SDK 2.16.8, and the problem is gone. So, it seems like the issue exists in the newer versions (SDK > 2.17
).
from mobx.dart.
Related Issues (20)
- Unable to use MultiReactionBuilder HOT 3
- [question] when is it mandatory to use runInAction
- Issue In ObservableList while using addAll method with iterables.
- Feature request: static analysis for empty `Observer`
- [Codegen] name used in generated mixin has to be constant to reduce compiled app size (especially important for Flutter Web) HOT 1
- [Question] No observables detected when checking in a ternary operator inside of a child parameter HOT 1
- Make the Observer to rebuild when no changes in immediate context HOT 2
- `ObservableSet` and `ObservableMap` notify all listeners when one is added with `fireImmediately: true`
- Observer widget doesnt observe the new state (Flutter web)
- late reactions not working HOT 5
- Error happened when building Observer, but it was captured since disableErrorBoundaries==true HOT 1
- Computeds not working when Observer in Observer HOT 6
- Can't use nullable type alias in computed; `null check operator used on null value` HOT 2
- ObservableMap not notifying Observer mobx 2.2.3 it was woking fine in 2.2.1 HOT 6
- feat: add requiresReaction to `Computed`
- feat: add keepAlive to Computed
- feat: add scheduler option to autorun and reaction HOT 1
- feat: add signal option to reactions
- How to wait ObservableFuture.status in function?
- testWidgets with Store not working properly HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mobx.dart.