Comments (6)
The basic ideas are outlined here: https://groups.google.com/d/msg/elm-discuss/xrmWjVobMcA/M0t8Xz3pCAAJ
@evancz I think these use cases (master-detail) are so common and the solution (maintaining state in the parent and passing it as props to the child) is so fundamental that it would be great to have another example discussing it.
from elm-architecture-tutorial.
A different approach worth considering is that of having a layout step.
Consider the case of a container who can lay out its children. (i.e. the positions and dimensions of the children are dictated by the container).
If the positions and dimensions of the children are included in its state, this could be problematic because you can easily go out of sync as these values depend on the position and dimensions of the parent and all of these values reside in the same object.
One solution would involve the following extensible type
type alias Layout a =
{ a | position : Vector
, size : Vector
}
-- type alias Vector = { x : Float , y : Float }
Now consider this example child component :
type alias State = { color : Color }
type Action = ...
update : Action -> State -> State
view : Address Action -> Layout State -> Html
and this example parent component :
type alias State =
{ children : List Child.State -- the above component state
, ...
}
type Action = Child Int Child.Action | NoOp
update : Action -> State -> State
layoutChild : Layout State -> Int -> Child.State -> Layout Child.State
-- to be used in conjunction with List.indexedMap
view : Address Action -> Layout State -> Html
Where the layoutChild
function would be applied somewhere in the view
function and would be responsible for determining the position and/or dimensions of the child.
I have yet to determine the limitations of this approach but here's an example gist demonstrating the approach : https://gist.github.com/TheSeamau5/002593199af89552a1bd
from elm-architecture-tutorial.
For what its worth, I would love to see the docs about best practices to structure parent - children relations.
from elm-architecture-tutorial.
This is a fantastic idea! So, the container itself is generic, but the components may be dropped in. So it can be a container of three selection lists, or a container of three buttons, or a container of three very complex forms. 👍 for including into Elm Architecture Tutorial.
from elm-architecture-tutorial.
The holy grail would be "a generic container for a selection list, a button and a complex form" ... which either requires one or another kind of extension to the type system, or requires some way of converging the types (for the container) so that what the container sees is three things that have the same type.
For the latter, what may work is some kind of intermediate type that can be constructed from the various child types. The trivial implementation is a union type, but it could be more complex than that.
from elm-architecture-tutorial.
Here's the beginning of advice on this kind of stuff: http://guide.elm-lang.org/reuse/
We will have more resources coming in the next few months, but that is the gist. If you are thinking about components, if you are thinking about parents and children and communication, something has already gone wrong.
Also, this community uses forums like elm-discuss and the Elm slack for discussions like this. Issues are for tracking concrete problems and work, not for discussions.
from elm-architecture-tutorial.
Related Issues (20)
- Could not find package elm-lang/html HOT 2
- Counters examples HOT 2
- Small error in 07-websockets.elm HOT 1
- major bug HOT 2
- 01-button.elm has syntax errors HOT 5
- Random example does not introduce the main function HOT 2
- Random example fails to import Random module HOT 3
- Disparity between docs and examples HOT 1
- Top-level value `main` does not have a type annotation. HOT 1
- Effects::Random Section Requires Subscription Section to Compile HOT 4
- HTTP tutorial doesn't make it clear how load additional http library HOT 2
- Checkboxes initial state ignored in labeled checkbox example HOT 1
- Password manager interferes with input fields
- JSON example has errors
- HTTP example contains errors HOT 1
- Example human time displayed is missing zero-padding
- json cat gif example broken HOT 2
- CORS error in 05-http.elm
- Compatibility with Elm 0.19.1 HOT 1
- Prompted to run 'Elm init' cmd when app already running
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elm-architecture-tutorial.