Comments (5)
I'm not sure I understand the use case.
- cortex already allows you to dynamically add callbacks to the root.
- If you need to perform side task after data updated why not do it in one of react lifecycle hooks, like
componentDidUpdate
The problem with the proposed solution is you'd need a different architecture to trigger callbacks to data at levels different from root. Imagine the parent Order component can also set quantity
of its child items, but provides a different callback than the set call in Item component. Or there's a decrease
method in Item with its own quantity set and callback. It's impossible to distinguish these different set calls since cortex does not know how data is used.
from cortex.
I think you described a good example of when adding a callback where the set
function is called is more convenient than a callback on root.
For example:
var Item = React.createClass({
increase: function() {
var quantity = this.props.item.quantity.getValue();
this.props.item.quantity.set(quantity + 1, function(){
this.alertThatActionIsFrom('increase button');
}.bind(this));
},
decrease: function(){
var quantity = this.props.item.quantity.getValue();
this.props.item.quantity.set(quantity - 1, function(){
this.alertThatActionIsFrom('decrease button');
}.bind(this));
},
alertThatActionIsFrom: function(origin){
window.alert("The subtotal is now " + this.subTotal() + " which resulted from a click to" + origin);
}
...
var Order = React.createClass({
increaseItem: function(item){
// Order can also increase item quantity but we don't want to have an alert for this
}
If we used componentDidUpdate
or cortex's callback on the root, we would need to work backwards to figure out what changed to determine if the alert should display 'increase button' vs 'decrease button'.
All this is already possible with what cortex or react provides us. I think adding a callback to the set
function would be more convenient?
Not too sure about the different architecture of triggering callbacks. Would it work if we added to this payload something like payload["callback"]?
Let me know what you think!
from cortex.
In your example, you have the callbacks displaying alerts, this is clearly views concern that should be handled by React. Don't sidestep React just because it's not immediately clear how to compute the diff.
There are many ways to figure out what changed, like keeping a state.changedValue or state.previousValue, or simply state.operation (plus
or minus), etc
Or better yet implement that extra logic as a sub component, AlertBox for instance, that takes data from Item and derive what to display.
Hope this helps
from cortex.
Hm, I can see how there are many ways of implementing my example. Maybe this is not a good enough example to demonstrate the use case.
Let me try one more argument! I can see how one would switch from plain React to using cortex to manage data. This should solve the problem of deeply nested callbacks which call setState
to update data. One would replace this with cortex's data and its set
method. So, I see a relation between cortex's set
methods and React's setProps
/setState
methods. But those methods take a callback to run when those batched state/props changes are done. Is there a way cortex can handle these callbacks as well?
Anyhoo, I thank you for your time and help! If you don't see a strong need for this, feel free to close!
from cortex.
That's an interesting idea, instead of waiting for data to be passed down, it is 'sent' to the right node. However, this means that every component must manage its own state/props and you have to supply callbacks to setState
and setProps
everywhere. At that point, I think you're not using React anymore. I think this approach is inferior to the current flow which allows you to pass data from the top down as props and you don't need to manually set anything since initialization and update look exactly the same.
To sum it up, I think if it's view related it should be handled by react, if it's a side task you can either hook into one of the lifecycle events or derive from the changed data. Neither of those requires the proposed callback approach, so I'm closing. Feel free to reopen if you come across something else.
from cortex.
Related Issues (20)
- cortextstore.splice not working as expected HOT 1
- cortexstore.val() out of synch with cortexstore.map() HOT 1
- Why not use Immutable.js? HOT 6
- How to handle cortex array efficiently? HOT 1
- can't remove a key of 0
- Why calling destroy on root level still trigger onUpdate callback? HOT 2
- Reference error after remove item from array using splice / destroy. HOT 5
- how to work with redux + react + webpack? HOT 1
- Tests not running HOT 3
- Unable to reset a value within the same tick HOT 9
- Using setProps to do the react update is depricated, per cortex intro HOT 4
- Please add a clearChange() to cortex HOT 1
- Deprecate getChanges and didChange HOT 1
- Add "this" argument to ArrayWrapper map HOT 1
- when call set() of an empty array, the first element of the new array will be remove HOT 1
- Usage with React propTypes HOT 5
- React Native support? HOT 1
- Please publish v2.0.2 HOT 6
- Please explain this part 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 cortex.