Created
February 2, 2018 08:19
-
-
Save heypoom/9619f4c600da66f9f8bf90386fbded83 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
// จะทำอะไร? | |
// 1. ชื่อ "การกระทำ" (Action Name) | |
ADD_TODO = 'ADD_TODO' | |
// 2. ข้อมูลทั้งหมดของแอพ (State) | |
state = {todos: ['เลือกตั้ง']} | |
// 3. การกระทำ (Action) | |
action = {type: ADD_TODO, payload: 'เตะประยุทธ์'} | |
// เปลี่ยนแปลงข้อมูล | |
;['ทักแชท', ...state.todos] | |
// 4. Reducer: ฟังก์ชั่นที่ รับการกระทำเข้าไป (Action) => ส่งข้อมูลใหม่ออกมา (State) | |
function todoReducer(state, action) { | |
if (action.type === ADD_TODO) { | |
return { | |
...state, | |
todos: [...state.todos, action.payload] | |
} | |
} | |
return state | |
} | |
todoReducer(state, action) | |
todoReducer({todos: []}, {type: 'ADD_TODO', payload: 'กินหนม'}) | |
// 5. ฟังก์ชั่นสร้างการกระทำ "Action Creator" | |
function addTodo(todo) { | |
return {type: ADD_TODO, payload: todo} | |
} | |
addTodo('กินขนม') | |
todoReducer(state, addTodo('กินขนม')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment