🌳
Walking though the Elm woods
  • Introduction
  • Structure of the book
  • Frequently asked questions
    • How can different types share data?
    • How to break Dependency Cycles?
    • How to structure an Elm project?
    • How to turn a Msg into a Cmd Msg?
    • How to update nested Records?
    • What are comparable types?
    • Why are Booleans bad?
    • 🔜Future topics
  • Recipes
    • Writing a Single Page Application
      • Share state across pages
      • Debounced Validation
      • Reusable views
    • Making impossible states Impossible
      • Non empty lists using Zippers
      • Restrict records using Opaque Types
      • Write safer functions using Phantom Types
    • Designing Elm package APIs
      • Create upwards compatible APIs
    • 🔜Future topics
  • Frameworks and packages
    • elm/parser
    • mdgriffith/elm-ui
    • 🔜Future topics
Powered by GitBook
On this page
  • Question
  • Answer

Was this helpful?

  1. Recipes
  2. Writing a Single Page Application

Share state across pages

PreviousWriting a Single Page ApplicationNextDebounced Validation

Last updated 5 years ago

Was this helpful?

Only Use Sparsely: For components within a page, use instead.

import Brwoser.Dom exposing (Viewport)
import Time exposing (Posix)

type Model =
    Loading
        { viewport : Maybe Viewport
        , time : Maybe Posix
        }
    | LoginPage
        { loginForm : LoginForm
        , viewport : Viewport
        , time : Posix
        }
    | AdminPage
        { userId : Int
        , viewport : Viewport
        , time : Posix
        }

tyep Msg =
    UpdatedViewport
    | UpdatedTime
    | ..

update : Msg -> Model -> (Model,Cmd Msg)
update msg Model =
    case Msg of
        UpdatedViewport ->
            --How can i updated the viewport in a scaleable way?
            
        UpdatedTime ->
            --How can i updated the time in a scaleable way?
import Brwoser.Dom exposing (Viewport)
import Time exposing (Posix)

type alias Config =
    { viewport : Viewport
    , time : Posix
    }

type Page =
    Login { loginForm : LoginForm }
    | Admin { userId : Int }

type Model =
    Loading
        { viewport : Maybe Viewport
        , time : Maybe Posix
        }
    | Ready ( Page , Config )

tyep Msg =
    UpdatedViewport Viewport
    | UpdatedTime Posix
    | ..

update : Msg -> Model -> (Model,Cmd Msg)
update msg model =
    case Msg of
        UpdatedViewport viewport ->
            case model of
                Loading m ->
                    {m | viewport = Just viewport}
                Ready (p,config) ->
                    ( p
                    , { config | viewport = viewport}
                    )
            
        UpdatedTime time->
            case model of
                Loading m ->
                    {m | viewport = Just time}
                Ready (p,config) ->
                    ( p
                    , { config | time = time}

Question

How should I handle shared state between pages

Answer

Define a config type and separate page definitions from the model definition.

reusable views