redux 可以使用多個不同 store 嗎?

redux 可以使用多個不同 store 嗎?

但是透過 google 一段時間之後,才深入去找問題的本身,發現 redux 作者希望,除非必要性的需求,否則盡量使用『單一 reduxer 對應到單一 store』 ,本持的精簡的精神處理物件。


可以採用以下的方式,用多個不同 reducer 包裝起來

// reducers.js<br></br>export step1 from './step1'<br></br>export step2 from './step2'<br></br><br></br>// constants.js<br></br>export const STEP1_COMPLETED = 'STEP1_COMPLETED'<br></br>export const STEP2_COMPLETED = 'STEP2_COMPLETED'<br></br><br></br>// step1.js<br></br>import { STEP1_COMPLETED } from './constants'<br></br><br></br>const initialStep1State = {<br></br> email: '',<br></br> password: '',<br></br>}<br></br><br></br>export function step1(state = initialStep1State, action) {<br></br> switch (action.type) {<br></br> case STEP1_COMPLETED:<br></br> return { ...state, email: }<br></br><br></br> default:<br></br> return state<br></br> }<br></br>}<br></br><br></br>// step2.js<br></br>import { STEP2_COMPLETED } from './constants'<br></br><br></br>const initialStep2State = {<br></br> firstname: '',<br></br> lastname: '',<br></br>}<br></br><br></br>export function step2(state = initialStep2State, action) {<br></br> switch (action.type) {<br></br> case STEP2_COMPLETED:<br></br> return { ...state, firstname: action.firstname, lastname: action.lastname }<br></br><br></br> default:<br></br> return state<br></br> }<br></br>}<br></br><br></br>// actions.js<br></br>import post from 'some-networking-lib'<br></br>import { STEP1_COMPLETED, STEP2_COMPLETED } from './constants'<br></br><br></br>export function saveStep1(data) {<br></br> return (dispatch) => {<br></br> post('/step1', data).then(({ email }) => {<br></br> dispatch({ type: STEP1_COMPLETED, email })<br></br> })<br></br> }<br></br>}<br></br><br></br>export function saveStep1(data) {<br></br> return (dispatch) => {<br></br> post('/step2', data).then(({ firstname, lastname }) => {<br></br> dispatch({ type: STEP2_COMPLETED, firstname, lastname })<br></br> })<br></br> }<br></br>}<br></br><br></br>// App.js<br></br>import { createStore, applyMiddleware } from 'redux';<br></br>import { Provider } from 'redux/react';<br></br>import * as reducers from './reducers';<br></br><br></br>const createStoreWithMiddleware = applyMiddleware()(createStore);<br></br>const store = createStoreWithMiddleware(reducers);<br></br><br></br>export default class App {<br></br> render() {<br></br> return (<br></br> <Provider store={ store }><br></br> { () => <FormWithStepsApp /> }<br></br> </Provider><br></br> );<br></br> }<br></br>}

ref, You don’t need multiple stores to achieve that but multiple reducers. …


資料中指出 redux , 建議採用 one store.

There are edge cases when you might use multiple stores (e.g. if you have performance problems with updating lists of thousands of items that are on screen at the same time many times per second). That said it's an exception, and in most apps you never need more than a single store.<br></br><br></br>Why do we stress this in the docs? Because most people coming from Flux will assume multiple stores is the solution to making update code modular. However Redux has a different solution for this: reducer composition.



Web developer, focus on website fullstack, special JavaScript, and love sharing developing experience and communicate with developers.