sdgfsdh
sdgfsdh

Reputation: 37131

Avoiding nesting boiler-plate in Elmish?

Suppose I have nested Elmish components like this: A contains B contains C.

Then if C has state and messages, these must be passed from C to B and then to A.

For example, the message and model types for B might be:

type Message = 
  | CMessage of C.Message
  | UpdateFoo of string

type Model = 
  {
    Foo : string
    C : C.Model
  }

Then update for B will do some routing:

let update message model = 
  match message with
  | CMessage m -> 
    {
      model with
        C = C.update m model.C 
    }
  | UpdateFoo foo -> { model with Foo = foo }

Then the same must be done for A consuming B messages.

This is quite verbose compared to setState, for example.

What are some strategies for managing this in Elmish?

Upvotes: 4

Views: 306

Answers (1)

Tyson Williams
Tyson Williams

Reputation: 1725

If you create a hierarchy of model types, then you need to route messages through that hierarchy in update. If you don't want to route messages through that hierarchy in update, then don't create a hierarchy of model types. That would mean you have a flat model.

However, I think that having a flat model is not sustainable. As your application grows in complexity, you will need to manage that complexity by grouping related concepts into (sub-)modules that contain their own model types.

This is quite verbose compared to setState, for example. What are some strategies for managing this in Elmish?

The function setState is meant for library authors. Your question is about specific model and message types, so my impression is that you are an "end user" of Elmish (instead of a library author). As such, I think it is unfair to compare your example update function to some alternative that uses setState since you should be using setState.

I think this verboseness you describe is the tradeoff for tightly controlling mutation. By using Elmish, you are saying that you want to tightly control mutation (by letting nearly all of it happen in Elmish instead of your code), so I think think this verboseness is necessary.

Upvotes: 2

Related Questions