Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Redux in Production

Redux in Production

Sam Yamashita

July 11, 2016
Tweet

More Decks by Sam Yamashita

Other Decks in Technology

Transcript

  1. 今日話すこと • Redux ͱ͸ʁ • Flux ͱ͸ʁ はじめに Growth Push

    • Growth Pushͱ͸ʁ • എܠ • վमલͷߏ੒ • վमޙͷߏ੒ • Switch ͩΒ͚ͷ Reducer • ࠶ར༻Մೳͳ Component
  2. –Dan Abramov “I didn't actually intend Redux to become a

    popular Flux library—I wrote it as I was working on my ReactEurope talk on hot reloading with time travel.” Reduxͱ͸ʁ Why use Redux over Facebook Flux?
  3. Reduxͱ͸ʁ Store 内の state とそのロジックを分離 todos: [“eat”, “sleep”] todos: [“”,

    “”] addTodo: (todo) => {} addTodo: (todo) => {} todos: [“eat”, “sleep”] addTodo: (todo) => {} todos: [“eat”, “sleep”] addTodo: (todo) => {} ߋ৽ 更新 更新 Before After ਤ A cartoon intro to Redux by Lin Clark
  4. Reduxͱ͸ʁ State を action から直接上書きしない After Action 1 Action2 Action

    3 state 1 state 2 state 3 ਤ A cartoon intro to Redux by Lin Clark
  5. Reduxͱ͸ʁ ਤ A cartoon intro to Redux by Lin Clark

    階層になっている state からなる 1つの大きな state After dispatcher reducer reducer reducer log reducer reducer reducer reducer reducer
  6. tree . └── reducers ├── index.js ├── application.js ├── notification.js

    ├── client.js ├── event.js └── tag.js Switch ͩΒ͚ͷ Reducer
  7. http://redux.js.org/docs/basics/Reducers.html Switch ͩΒ͚ͷ Reducer 1 import * as ActionTypes from

    ' ../actions/todoApp'; 2 3 function todoApp(state = initialState, action) { 4 switch (action.type) { 5 case ActionTypes.SET_VISIBILITY_FILTER: 6 return { 7 ...state, 8 visibilityFilter: action.filter 9 }; 10 default: 11 return state 12 } 13 }
  8. https://github.com/acdlite/redux-actions Switch ͩΒ͚ͷ Reducer 1 const todoApp = handleActions({ 2

    SET_VISIBILITY_FILTER: (state, action) => ({ 3 visibilityFilter: action.filter 4 }), 5 }), initialState );
  9. Before After Switch ͩΒ͚ͷ Reducer 1 import * as ActionTypes

    from ' ../actions/todoApp'; 2 3 function todoApp(state = initialState, action) { 4 switch (action.type) { 5 case ActionTypes.SET_VISIBILITY_FILTER: 6 return { 7 ...state, 8 visibilityFilter: action.filter 9 }; 10 default: 11 return state 12 } 13 } 1 const todoApp = handleActions({ 2 SET_VISIBILITY_FILTER: (state, action) => ({ 3 visibilityFilter: action.filter 4 }), 5 }), initialState );
  10. ࠶ར༻Մೳͳίϯϙʔωϯτ Container Components Presentational and Container Components • Redux であれば

    connect() を使用してStore をもらうところ • スタイルをもたない傾向にある • 余計な DOM を含まない傾向にある • 例えば、 ユーザーページ、ログインページ
  11. ࠶ར༻Մೳͳίϯϙʔωϯτ Presentational Components Presentational and Container Components • functional component

    で実装できる傾向がある • スタイルをもちます • 実際の DOM を含みます • 例えば、 リスト、モーダル
  12. ࠶ར༻Մೳͳίϯϙʔωϯτ Presentational and Container Components 1 const TodoList = ({

    todos }) => ( 2 <ul> 3 {todos.map(todo => 4 <Todo 5 key={todo.id} 6 { ...todo} 7 /> 8 )} 9 </ul> 10 ); 1 class todoPage extends React.Component { 2 render() { 3 return ( 4 <div> 5 <TodoList todos={this.props.todos} /> 6 </div> 7 ) 8 } 9 }
  13. ࠷ޙʹ • ドキュメントが充実してる • 日本語だったら @kuy さんの Qiita にある記事とか •

    サンプルプロジェクトがたくさん • コミュニティが充実してる • サンプルプロジェクト作ってみた(全然間に合わなかった) • https://github.com/sotayamashita/curator