Nested model reducer question
See original GitHub issueI’m unsure how to update just a part of a complex model and could not find any examples. This is a bit contrived, but what would the reducer look like that changes the name property on the “d” object. What’s the best practice for something like this? Thanks!
app({
model: {
a: {
name: "a",
b: {
name:"b",
c: {
name: "c",
d: {
name:"d",
e: {
name:"e",
f: {
name:"f"
}
}
}
}
}
}
},
reducers: {
changeDsName: (val) => ({ /* not sure what goes here */ })
},
view: (model, actions) => (
<div>
<button onclick={ _ => actions.changeDsName("g") }>Change D</button>
<div>{ model.a.b.c.d.name }</div>
</div>
)
})
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
F-tests and Nested Models - Rose-Hulman
Assessing Predictors and Testing Coefficients via Nested Models: By comparing a given reduced model with the complete model we can assess the usefulness...
Read more >Nested reducers - javascript - Stack Overflow
Reducers should never dispatch actions, or do anything else that has side effects. All a reducer should ever be is a pure function...
Read more >Nested models? · Issue #265 · rematch/rematch - GitHub
Hey - for a starter, I'd like to say that I do like rematch, awesome work! ;) Now, the question part - is...
Read more >Deeply Nested Objects and Redux | Pluralsight
With deeply nested objects, the workload (of both human and machine) ... In Redux, this would mean using separate reducers (or different ...
Read more >Practical Redux, Part 11: Nested Data and Trees
Another big question is where the logic for default model attribute values should live, and how to override those defaults when a new...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@cdeutmeyer Well, hyperapp only expects a new, modified model. So I’d say the question itself isn’t really related to hyperapp. It’s a question about this form of recursive data-structure.
I’d say answering that you need 2 things:
Funny thing is that coincidentally both of these algorithms are basically doing the same thing. I personally would use a function that you call recursively for all keys in the current sub-object you’re in. Then that function could either clone or modify its part and return the result.
If you don’t know what such a function would look like I suggest googling for “JavaScript object deep clone”. That’ll lead a ton of results and most of them will be recursive.
@cdeutmeyer This looks good.
From the docs:
So, HyperApp is doing
Object.assign({}, model, newModel)
for you already.Your example:
Is equivalent to: