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.

Q: Is it possible to use redux-style state management?

See original GitHub issue

I’m curious is it possible with Laminar to have whole app state in one big immutable object, like in Redux or ClojureScript’s re-frame?

For example, I have something like:

case class Todo(...)
case class User(...)
case class AppState(user: User, todos: Seq[Todo])

As far as I can understand to build view layer for this app-state we need something like (sorry, don’t know well Laminar API, so I use pseudo-code):

def appView(state: ReactiveStream[State]): ReactiveNode[Div] = {
  // .. here we call child components userView(user: ReactiveStream[User])
  //    and todoView(todo: ReactiveStream[Todo]) ...
}

Does it have any sense? If not, what’s the recommended way to manage state with event-stream based frameworks?

Btw, thanks for your great work! No doubts we need Scala-“native” solutions to cope with UI issues and I found Laminar and Outwatch very promising! 👍

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
raquocommented, Apr 23, 2018

No, extraneous re-renders should not be needed at all. In your code you will have pieces like something <-- someSignal. That means that when someSignal emits a new value, something will be updated in the DOM, nothing more.

In your user component you will probably have something like div(child.text <-- usernameSignal) where usernameSignal is derived from centralVar. So when usernameSignal updates, a new DOM text node will be created and inserted into that div instead of the previous text node.

I assume nothing in the todo list depends on the username, so its DOM will not be touched at all when it updates.

The thing to avoid in Laminar is this pattern: div(child <-- usernameSignal.map(username => span(username))). In that case, a new span element will be created every time usernameSignal updates. That is not a big deal in my contrived example because username updates are probably very rare, and this span is just one element, but if it was a bigger component and/or the updates were coming in more often that would be really inefficient.


Importantly, Airstream’s State does not emit a new value if it is equal to its previous value. So you should make your components depend on individual chunks of centralVar (e.g. centralVar.user) rather than on the centralVar itself, because the value of centralVar itself will change with every update to any of centralVar’s contents because of immutability, but the individual chunks will only change when they themselves (or their children) change.

Note that Signal-s don’t have the same de-duplication behaviour built-in. I wasn’t sure about that design decision, and I might add deduplication later. For now you’ll need to .map centralVar first to narrow it down, and only then convert it to a Signal, otherwise you’ll get more DOM updates than needed.

0reactions
raquocommented, Mar 14, 2020

State management is now sufficiently documented under Var in Airstream docs I think.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux Style Guide
Redux Style Guide. Introduction​. This is the official style guide for writing Redux code. It lists our recommended patterns, best practices ...
Read more >
Learn Redux — Introduction To State Management With React
Redux is a predictable state container for JavaScript and comes as an external library which can be used together with front-end frameworks ...
Read more >
How To Manage State in React with Redux - DigitalOcean
In this tutorial, you'll use Redux in a React application by building a bird watching test application. Users will be able to add...
Read more >
React state management: What is it and why to use it?
Learn about React state management and why we need state management and how to use it with React hooks and Redux in the...
Read more >
Pure Redux - Dave Ceddia
React Context for State Management · Use Context to avoid prop drilling · Allow Children to Update Parents with a callback · Hide...
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