國立彰化師範大學 資工系
創科資訊實習生
View
( React )
Store
Action
Reducer
dispatch
畫面需要改變
return
newState
資料變更
Server
{
todos: [
{
text: 'todo1',
completed: true,
},
],
}
const ADD_TODO = 'ADD_TODO';
// action
{
type: ADD_TODO,
text: 'Build my first Redux app',
}
// action creator
function requestAddTodo() {
return {
type: ADD_TODO,
text: 'Build my first Redux app',
};
}
function todos(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [...state, {
text: action.text,
completed: false
}];
default:
return state;
}
}
store = {
todos: [{
text: 'todo1 title',
completed: false,
},{
text: 'todo2 title',
completed: true,
}],
}
{ conbineReducers } = redux
容器組件 | 展示組件 | |
---|---|---|
Location | 最頂層,路由處理 | 中間和子組件 |
Aware of Redux | 是 | 否 |
讀取數據 | 從 Redux 獲取 state | 從 props 獲取數據 |
修改數據 | 向 Redux 派發 actions | 從 props 調用回調函數 |