Comments (5)
from flutter_redux.
Example:
import 'package:flutter/material.dart';
import 'package:redux/redux.dart';
enum Actions { increment }
int counterReducer(int state, dynamic action) {
return action == Actions.increment ? state + 1 : state;
}
void main() {
runApp(StoreProvider<int>(
child: MaterialApp(
home: Scaffold(
body: StoreConnector<int, VoidCallback>(
builder: (context, vm) {
print("render");
vm();
return const Text("data");
},
converter: (store) => () => store.dispatch("Foo")))),
store: store));
}
Restarted application in 927ms.
2064 I/flutter (17550): render
Application finished.
Exited (sigterm)
from flutter_redux.
I believe you're looking for the onInit
callback function which can be passed to the StoreConnector
constructor.
https://pub.dev/documentation/flutter_redux/latest/flutter_redux/StoreConnector/onInit.html
If you dispatch an action inside the builder
function, it will send that action to the Redux reducer, which triggers a state change, which triggers a rebuild, which runs the builder
function, which dispatches again, which calls the recuer, produces a state changes, calls the builder... and you've found yourself in an infinite loop :)
Some sample code below (might be missing a paren somewhere -- github coding and didn't run it locally, but should get the idea across).
import 'package:flutter/material.dart';
import 'package:redux/redux.dart';
enum Actions { increment }
int counterReducer(int state, dynamic action) {
return action == Actions.increment ? state + 1 : state;
}
void main() {
runApp(StoreProvider<int>(
store: store
child: MaterialApp(
home: Scaffold(
body: StoreConnector<int, int>(
// Grab the current count from the store
converter: (store) => store.state,
// A function that runs once when the StoreConnector is inserted into the widget tree
onInit: (store) => store.dispatch(increment),
// Should be a pure function that returns a Widget tree and performs no side effects.
builder: (context, counter) {
return const Text("current count $counter");
},
),
),
);
}
from flutter_redux.
Thanks @brianegan totally forgot onInit. It solves what i'm trying to do.
However, is it intended that when new state and old state is being reduced will still produce a rerender? Isn't that a bit wasteful?
from flutter_redux.
Thanks! I'll have a look and reach out if I need more help. Much appreciated.
from flutter_redux.
Related Issues (20)
- How to not accept change notifications when the Router is not at the top level, and then take the initiative to obtain the changes when the Router returns to the top level HOT 1
- onDidChange called twice HOT 8
- Is this package still actively maintained?
- `onWillChange` and `onDidChange` errors are swallowed HOT 4
- Warning related to `?` operator on Linux/Dev channel. HOT 2
- Import Store data type
- Widgets binding error after upgrading flutter version to 2.13.0-0.1.pre HOT 1
- onDidChange not working as expected HOT 3
- Build warning with Flutter 3.0 HOT 5
- flutter_redux depends on flutter_redux, version solving failed HOT 3
- onWillChange and onDidChange called twice after state update HOT 2
- Passing Store as an argument to MethodChannel and Background Isolate
- Question on how to use the reducer right way HOT 2
- [Question]: Any thoughts on difference between ignoreChange and distinct?
- Socket and Flutter_redux HOT 18
- How to watch redux store change inside useEffect? HOT 5
- [Question] is it expected that the store doesn't run until frames render? HOT 1
- After push a new page, and Dispatching an action, there is an infinite loop
- Is flutter redux actively maintained HOT 1
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 flutter_redux.