All Projects → afcastano → elm-nested-component-communication

afcastano / elm-nested-component-communication

Licence: other
Example of Sibling/Nested communication in elm

Programming Languages

elm
856 projects

Projects that are alternatives of or similar to elm-nested-component-communication

elm-from-ground-up
This lab is designed to be an adjunct to learning Elm. Go from the most basic "Hello World" through data types and more advanced features in this example.
Stars: ✭ 17 (-5.56%)
Mutual labels:  elm-architecture, elm-demos
Elmdroid
Minimalistic Android implementation of The Elm Architecture with android architecture components integration.
Stars: ✭ 25 (+38.89%)
Mutual labels:  elm-architecture
Javascript Todo List Tutorial
✅ A step-by-step complete beginner example/tutorial for building a Todo List App (TodoMVC) from scratch in JavaScript following Test Driven Development (TDD) best practice. 🌱
Stars: ✭ 212 (+1077.78%)
Mutual labels:  elm-architecture
mpeiapp
MpeiX - Расписание пар, карта корпусов и личный кабинет БАРС для студентов и преподавателей НИУ МЭИ
Stars: ✭ 19 (+5.56%)
Mutual labels:  elm-architecture
Gipher
tinder like app for gifs built with elm and firebase
Stars: ✭ 229 (+1172.22%)
Mutual labels:  elm-architecture
larch
An Elm-like ClojureScript framework
Stars: ✭ 37 (+105.56%)
Mutual labels:  elm-architecture
Philip2
An Elm to OCaml compiler
Stars: ✭ 182 (+911.11%)
Mutual labels:  elm-architecture
concur-replica
Server-side VDOM UI framework for Concur
Stars: ✭ 136 (+655.56%)
Mutual labels:  elm-architecture
gruid
Cross-platform grid-based UI and game framework.
Stars: ✭ 67 (+272.22%)
Mutual labels:  elm-architecture
whistle
Experiment to build single page apps in Elixir
Stars: ✭ 52 (+188.89%)
Mutual labels:  elm-architecture
elmish-getting-started
A simple and minimalistic template to easily get up and running with Elmish and Fable
Stars: ✭ 22 (+22.22%)
Mutual labels:  elm-architecture
Concur
An unusual Web UI Framework for Haskell
Stars: ✭ 252 (+1300%)
Mutual labels:  elm-architecture
tea-chess
A chess-themed tutorial on writing an SPA in Bucklescript-TEA
Stars: ✭ 28 (+55.56%)
Mutual labels:  elm-architecture
Hydux
A light-weight type-safe Elm-like alternative for Redux ecosystem, inspired by hyperapp and Elmish
Stars: ✭ 216 (+1100%)
Mutual labels:  elm-architecture
Teapot
Unidirectional Dataflow library for Android inspired by The Elm Architecture
Stars: ✭ 29 (+61.11%)
Mutual labels:  elm-architecture
Render
UIKit a-là SwiftUI.framework [min deployment target iOS10]
Stars: ✭ 2,150 (+11844.44%)
Mutual labels:  elm-architecture
elm-double-folding-pattern
Actions + Events = 🐉
Stars: ✭ 29 (+61.11%)
Mutual labels:  elm-architecture
re-alm
An Elm Architecture experiment in ClojureScript
Stars: ✭ 24 (+33.33%)
Mutual labels:  elm-architecture
pokerex client
Elm client for PokerEx project
Stars: ✭ 39 (+116.67%)
Mutual labels:  elm-demos
UniTEA
Implementation of The Elm Architecture for Unity3D
Stars: ✭ 31 (+72.22%)
Mutual labels:  elm-architecture

Example of Sibling/Nested component communication in elm.

The example tries to implement the accepted answer of this StackOverflow question

Please create issues with any feedback or leave comments in the SO question.

Working example:

http://afcastano.github.io/elm-nested-component-communication/

Problem:

  • I have one parent component with two children. With the Elm Architecture, how can I update the right child when any of the counters in the left child change? Avoid the parent component to know about the internals of the children since it is not scalable.

Proposed solution:

  • Each component will provide functions to access the data the parent needs and also expose Msg to update nested data. In this way, the parent component only need to know about the exposed functions and Msg of the direct child.

Key parts of the implementation

Counter.elm exposes getNum function to return the value of the counter without knowing the internal structure and a new SetNum Msg to update it:

type Msg
    = Increment
    | Decrement
    | SetNum Int

...

getNum : Model -> Int
getNum model =
    model.num

Pair.elm also exposes a function to return the red number:

getRedNum : Model -> Int
getRedNum model =
    Counter.getNum model.redCounter

and a new update Msg that is propagated to the update function of the Counter.

type Msg
    = PairRed Counter.Msg
    | PairGreen Counter.Msg
    | UpdateRed Int


update : Msg -> Model -> Model
update msg model =
    case msg of
        UpdateRed value ->
            { model | redCounter = Counter.update (Counter.SetNum value) model.redCounter }

Main.elm orchestrates the whole thing. Whenever a counter changes, it updates the other counter and the totals without knowing the internal structure of neither of them:

Pair1 subMsg ->
    let
        pair1 =
            Pair.update subMsg model.pair1

        totals =
            Totals.update (Totals.UpdateRed <| Pair.getRedNum pair1) model.totals

        pair2 =
            Pair.update (Pair.UpdateRed <| Pair.getRedNum pair1) model.pair2
    in
        Model pair1 pair2 totals

Running the example

To run it, simply do:

git clone [email protected]:afcastano/elm-nested-component-communication.git
cd elm-nested-component-communication
elm-reactor

go to http://localhost:8000/src/Main.elm


Thanks to rofrol for all the corrections made to this example.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].