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 actions and sliced state

See original GitHub issue

Heya! Sliced state concept is pretty fantastic by idea and is pretty unique to HyperApp, i believe. But.

There’s one tricky and big BUT. Consider such example


app({
  state: {
    title: 'abc',
    foo: {
      hello: 'world',
      todo: {
        list: [],
        count: 0,
        done: 0,
      },
      bar: {
        xxx: 'xxx',
        baz: {
          zzz: 'zzz',
          qux: {
            hi: 'buddy',
            aa: 'bb',
          },
        },
      },
    },
  },
  actions: {
    // full state: state.title, state.foo, state.foo.hello and etc
    zazy: ({ state }) => {},
    foo: {
      // state.todo only
      todo: ({ state, actions }) => () => ({ count: state.list.length + 1 }),

      // state.bar only
      bar: {
        // access to: state.zzz & state.qux
        baz: ({ state, actions }) => () => ({ zzz: state.zzz + 'yyy' }),
      },
    },
  },
});

It turns out that when you have nested action named the same as some key in the state, then you partially are not able to access to upper state? Notice the foo - action and state. In the foo action we have two nested actions which want to deal with slice state separately, but because foo is object of actions then we can’t get state.foo.hello for example. The only way that i see to access state.title and/or state.foo.hello is to create another “top-level” action which in turn can be access them and change them if that action is reducer (returning function).

It may be confusing, but this concept is pretty powerful and super awesome! Congrats.

By the way, did you realize that you can pass return of an app() call directly to an action, haha? 🤣

app({
  state: {
    hello: 'world',
    todo: {
      list: [],
      count: 0,
      done: 0,
    },
  },
  actions: {
    // state.hello & state.todo
    foo: (state, actions) => {},
    todo: app({
      actions: {
        // state.list, state.count & state.done
        add: (state, actions) => {},
        del: (state, actions) => {},
        list: (state, actions) => {},
      },
    }),
  },
});

btw, not sure if that work 😄 because probably app not works without state? May be kinda kool too 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
zacenocommented, Nov 3, 2017

@olstenlarck Not so many discussions really. That example I gave came out of a discussion on slack between me & @JorgeBucaran where I was arguing for the value of an event bus as a solution to the problem (which hyperapp used to have but recently took out), while Jorge was arguing for the aforementioned solution.

I think Jorge’s approach is the right one for most cases, but I still believe there are situations where an event bus helps keep things nice and separate. If you’re curious about that you could look into https://github.com/zaceno/hyperapp-events

1reaction
tunnckoCorecommented, Nov 3, 2017

It isn’t really an issue. Initially it was named “am i understanding * correctly?”. Just playing with the things that i thought from the first time when i seen that concept introduced. So, yea, i understanding it correctly and i will just need to add NON-matching-to-state-key to actions.foo like

app({
  // ...
  actions: {
    foo: {
      someKey: (state, actions) => {} // to access `state.foo`
    }
  }
})

@zaceno haha, good comment. Yea i got it immediately when seen it for the first time. My “problem”(actually was not so clear in my mind) how can get the state of actions.foo when i also want to have nested action in it for accessing and modifying nested state ;d But while writing the snippets i reallized how 😄

But yea, after playing with cup of coffee and starting the issue it was clarified 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Suggestion: add syntax or examples for nested slices #259
Basically I want to have pieces of state in my slice that are managed by other slices. In the same sense that the...
Read more >
How to cut the boilerplate for nested Redux state - Jakob Lind
So you have this nested structure in your Redux state: It's a list of users, and each user can have many comments. Your...
Read more >
Actions in multiple slices in Redux toolkit - Stack Overflow
The first argument is the name of the slice, the second is the slice of state and the third is the initial state....
Read more >
Redux Fundamentals, Part 3: State, Actions, and Reducers
The official Redux Fundamentals tutorial: learn how reducers update state in response to actions.
Read more >
How To Setup Redux Slices with Redux Toolkit - SoftKraft
... a slice name, and an initial state value and lets us auto-generate action types and ... We can use useSelector() multiple times...
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