question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested model reducer question

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
FlorianWendelborncommented, Feb 27, 2017

@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:

  1. a cloning algorithm, since technically hyperapp reducers are supposed to be pure functions, which means that the model is immutable
  2. an algorithm that iterates the object to find the spot that has to be changed

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.

0reactions
jorgebucarancommented, Feb 27, 2017

@cdeutmeyer This looks good.

From the docs:

Reducers return a new model or part of it. If it returns part of a model, that part will be merged with the current model.

So, HyperApp is doing Object.assign({}, model, newModel) for you already.

Your example:

changeGreeting : (model, g) => ({ greeting: Object.assign({}, model.greeting, { happy: g }) })

Is equivalent to:

changeGreeting : (_, g) => ({ happy: g })
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found