-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.js
67 lines (59 loc) · 2.11 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// This is the simplest example of a general user interface pattern I will be
// showing you. It is wholely inspired by the Elm Architecture:
// https://github.com/evancz/elm-architecture-tutorial/
// This is a simple counter app that uses React as a declarative rendering
// "service". A service takes some declarative structure and does a bunch of
// nasty mutations and side-effects that we want to hide from the main logic
// of the application.
import React from 'react'
import ReactDOM from 'react-dom'
// The simplest UI component consists of 3 pure, side-effect-free functions
// with the following type signatures:
//
// init : () => state
// update : state => action => state
// view : dispatch => state => html
// the initial state of the counter is 0
// init : () => state
const init = () => 0
// `update` takes the current state and an action, and returns a new state.
// This counter has two actions, increment and decrement.
// update : state => action => state
const update = (state, action) => {
switch (action) {
case 'increment':
return state + 1
case 'decrement':
return state - 1
default:
console.warn("Unknown action:", action)
return state
}
}
// `dispatch` is a function that accepts actions that will be passed to `update`
// `view` returns a react component that sends actions to dispatch.
// dispatch : action => ()
// view : dispatch => state => html
const view = (dispatch, state) => {
const inc = () => dispatch('increment')
const dec = () => dispatch('decrement')
return (
<div>
<button onClick={dec}>-</button>
<span>{state}</span>
<button onClick={inc}>+</button>
</div>
)
}
const app = {init, update, view}
const root = document.getElementById('root')
// now we just need to connect the pieces.
let state = app.init()
// when we get an action, we want to update the state and re-render the ui
const dispatch = action => {
state = app.update(state, action)
ReactDOM.render(app.view(dispatch, state), root)
}
// the initial render
ReactDOM.render(app.view(dispatch, state), root)
// that's it! get it running with: `npm i && node server.js`