Last active
August 31, 2018 03:04
-
-
Save potato4d/3de0fdff65e9ced8acab05e9a94440e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 現実 | |
| const { app, h } = hyperapp | |
| /** @jsx h */ | |
| const state = { | |
| isInited: false, | |
| users: [] | |
| } | |
| const actions = { | |
| fetchUsers: () => (_, actions) => { | |
| axios.get('https://jsonplaceholder.typicode.com/users') | |
| .then(({ data }) => { | |
| actions.setUsers({ users: data }) | |
| actions.setInited() | |
| }) | |
| }, | |
| setInited: () => () => ({ | |
| isInited: true | |
| }), | |
| setUsers: ({ users }) => () => ({ | |
| users | |
| }) | |
| } | |
| const view = (state, actions) => { | |
| if (!state.isInited) { | |
| actions.fetchUsers() | |
| return (<div></div>) | |
| } | |
| console.log(state.users) | |
| return ( | |
| <div> | |
| <ul> | |
| {state.users.map((user) => (<li>{user.name}</li>))} | |
| </ul> | |
| </div> | |
| ) | |
| } | |
| app(state, actions, view, document.body) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 動いてほしいやつ | |
| const { app, h } = hyperapp | |
| /** @jsx h */ | |
| const state = { | |
| isInited: false, | |
| users: [] | |
| } | |
| const actions = { | |
| fetchUsers: () => async () => { | |
| const { data } = await axios.get('https://jsonplaceholder.typicode.com/users') | |
| return { | |
| isInited: true, | |
| users: data | |
| } | |
| } | |
| } | |
| const view = (state, actions) => { | |
| if (!state.isInited) { | |
| actions.fetchUsers() | |
| return (<div></div>) | |
| } | |
| return ( | |
| <div> | |
| <ul> | |
| {state.users.map((user) => (<li>{user.name}</li>))} | |
| </ul> | |
| </div> | |
| ) | |
| } | |
| app(state, actions, view, document.body) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Jorge に聞きたいこと
setStateを自力で生やすのはアリ?ナシ?それぞれのアクションが 状態に関与しないアクション なのか 状態に関与するアクション なのかは明確に区別されていないとアクションという概念が著しく可読性を下げる気がしてる。なので状態の変更は setState アクションをもってのみ行うことができるような形で書くと最低限の治安が保たれる気がしているけれどどうだろう 🤔