Redux 學習筆記 — node.js開發
最近開始學習node.js開發,有很多基礎知識和框架需要學習,所以打算將自己學習筆記記錄下來,即可以和大家分享,也可以方便自己以後回顧知識。由於只是個人筆記,便不是詳細的教程,有不懂的代碼還請自行百度。
主要模塊
- redux
- redux-thunk
- react-redux — 配合react
- redux-logger — 調試工具,中間組件
代碼段
'use strict'
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk'
import createLogger from 'redux-logger'
import rootReducer from '../reducers'
export default function configureStore(preloadedState) {
const store = createStore(
rootReducer,
preloadedState,
compose(
applyMiddleware(thunk, createLogger())
)
)
return store
}
配置store,applyMiddleware綁定中間組件
import ActionTypes from './types';
export function addHistory(history) {
return {
type: ActionTypes.ADD_HISTORY,
history
}
}
基本的action結構
import ActionTypes from '../actions/types';
const initialState = [
{ text: 'Use Redux' }
];
function post(state = initialState, action) {
switch (action.type) {
case ActionTypes.ADD_HISTORY:
return [
{ text: action.text },
...state
];
...
default:
return state;
}
}
基本的reduce結構