Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The idea of smart and dumb components in Elm

Tags:

elm

I like React's/Redux' concept of smart and dumb components, where a dumb component does not handle its own state (Dump component does not know anything about the outside world, all it does is triggering an event and displaying value according to its input). This is trivial because all state in handled in one place (main reducer).

In Elm, each component has its own 'update' function (similar to Redux' reducer), so it does not seem trivial to use the same (dumb & smart components pattern).

Is using smart & dump components a good practice in Elm? If so, will I have components without 'update' method? I wonder how will I pass data (props) to my component and how will I trigger events to the parent component.

I will love to hear your thoughts.

like image 842
Yaniv Efraim Avatar asked Apr 23 '16 19:04

Yaniv Efraim


People also ask

What is smart and dumb component?

A Component that performs nothing on its own and is completely reliant on Smarter Components. Dumb Components just exist to present something in the DOM. As a result, they are sometimes known as “Presentational Components” or “Isolated Components.” import{ Component, Input, OnInit } from '@angular/core'; @Component({

Which of the following is referred as dumb component?

Dumb components are also called 'presentational' components because their only responsibility is to present something to the DOM. Once that is done, the component is done with it. No keeping tabs on it, no checking in once in a while to see how things are going.

What is a dummy component?

Dummy Components are the exact mechanical equivalent of fully functional, electronically active components. Dummy SMD components are available in all popular IC fine pitch packages including PoP, TMV, WLP, BGA, Flip Chip, CSP, QFP, LQFP, TSOP, TSSOP, SSOP, PLCC and also resistors and capacitors.

What is smart component in angular?

Smart Components are those components who know how to handle data, How to fetch data from services, How to interact with services and keep track of the state and care about how the app works as a whole. They are called containers as they send data to Dumb Components via property bindings.


1 Answers

The Elm equivalent of a "dumb component" (a.k.a. Presentational, Pure, Skinny component) is simply a function that produces Html:

view : ... -> Html

The elm-html library is written in this style, e.g.,

button : List Attribute -> List Html -> Html

You set the "props" by providing attributes when you call the function. In particular, you register events by supplying handlers in List Attribute:

button
  [ onClick addr ClickAction ]  -- event handler
  [ text "Click me" ]           -- child "components"

You'll see this pattern also in other libraries, although the exact types may be different from List Attribute and List Html.

like image 103
Søren Debois Avatar answered Oct 04 '22 23:10

Søren Debois