Redux
  • Read Me
  • 소개
    • 동기
    • Core Concepts
    • 3가지 원칙
    • 기존 기술들
    • Learning Resources
    • 생태계
    • 예시
  • 기초
    • 액션
    • 리듀서
    • 스토어
    • 데이터 흐름
    • React와 함께 사용하기
    • 예시: Todo List
  • 심화
    • 비동기 액션
    • 비동기 흐름
    • 미들웨어
    • React Router와 함께 사용하기
    • 예시: Reddit API
    • Next Steps
  • 레시피
    • Configuring Your Store
    • Redux로 마이그레이션
    • 객체 확산 연산자 사용하기
    • 보일러플레이트 줄이기
    • Server Rendering
    • Writing Tests
    • Computing Derived Data
    • Implementing Undo History
    • Isolating Subapps
    • 리듀서 구조화하기
      • 사전에 요구되는 개념들
      • 기본 리듀서 구조
      • 리듀서 로직 분리하기
      • 리듀서 예제 리팩토링하기
      • combineReducers 사용하기
      • combineReducers 더 알아보기
      • 상태 정규화하기
      • 정규화된 데이터 업데이트하기
      • 리듀서 로직 재사용하기
      • 불변 업데이트 패턴
      • 상태 초기화하기
    • Using Immutable.JS with Redux
  • FAQ
    • General
    • Reducers
    • Organizing State
    • Store Setup
    • Actions
    • Immutable Data
    • Code Structure
    • Performance
    • Design Decisions
    • React Redux
    • Miscellaneous
  • 문제해결
  • 용어사전
  • API 레퍼런스
    • createStore
    • Store
    • combineReducers
    • applyMiddleware
    • bindActionCreators
    • compose
  • 변경 기록
  • 후원자
  • 피드백
Powered by GitBook
On this page
  • Table of Contents
  • Store Setup
  • Can or should I create multiple stores? Can I import my store directly, and use it in components myself?
  • Is it OK to have more than one middleware chain in my store enhancer? What is the difference between next and dispatch in a middleware function?
  • How do I subscribe to only a portion of the state? Can I get the dispatched action as part of the subscription?
  1. FAQ

Store Setup

PreviousOrganizing StateNextActions

Last updated 6 years ago

Table of Contents

Store Setup

Can or should I create multiple stores? Can I import my store directly, and use it in components myself?

The original Flux pattern describes having multiple “stores” in an app, each one holding a different area of domain data. This can introduce issues such as needing to have one store “waitFor” another store to update. This is not necessary in Redux because the separation between data domains is already achieved by splitting a single reducer into smaller reducers.

As with several other questions, it is possible to create multiple distinct Redux stores in a page, but the intended pattern is to have only a single store. Having a single store enables using the Redux DevTools, makes persisting and rehydrating data simpler, and simplifies the subscription logic.

Some valid reasons for using multiple stores in Redux might include:

  • Solving a performance issue caused by too frequent updates of some part of the state, when confirmed by profiling the app.

  • Isolating a Redux app as a component in a bigger application, in which case you might want to create a store per root component instance.

However, creating new stores shouldn't be your first instinct, especially if you come from a Flux background. Try reducer composition first, and only use multiple stores if it doesn't solve your problem.

Similarly, while you can reference your store instance by importing it directly, this is not a recommended pattern in Redux. If you create a store instance and export it from a module, it will become a singleton. This means it will be harder to isolate a Redux app as a component of a larger app, if this is ever necessary, or to enable server rendering, because on the server you want to create separate store instances for every request.

With , the wrapper classes generated by the connect() function do actually look for props.store if it exists, but it's best if you wrap your root component in <Provider store={store}> and let React Redux worry about passing the store down. This way components don't need to worry about importing a store module, and isolating a Redux app or enabling server rendering is much easier to do later.

Further information

Documentation

Discussions

Is it OK to have more than one middleware chain in my store enhancer? What is the difference between next and dispatch in a middleware function?

Redux middleware act like a linked list. Each middleware function can either call next(action) to pass an action along to the next middleware in line, call dispatch(action) to restart the processing at the beginning of the list, or do nothing at all to stop the action from being processed further.

This chain of middleware is defined by the arguments passed to the applyMiddleware function used when creating a store. Defining multiple chains will not work correctly, as they would have distinctly different dispatch references and the different chains would effectively be disconnected.

Further information

Documentation

Discussions

How do I subscribe to only a portion of the state? Can I get the dispatched action as part of the subscription?

Redux provides a single store.subscribe method for notifying listeners that the store has updated. Listener callbacks do not receive the current state as an argument—it is simply an indication that something has changed. The subscriber logic can then call getState() to get the current state value.

The new state is not passed to the listeners in order to simplify implementing store enhancers such as the Redux DevTools. In addition, subscribers are intended to react to the state value itself, not the action. Middleware can be used if the action is important and needs to be handled specifically.

Further information

Documentation

Discussions

Libraries

This API is intended as a low-level primitive with no dependencies or complications, and can be used to build higher-level subscription logic. UI bindings such as React Redux can create a subscription for each connected component. It is also possible to write functions that can intelligently compare the old state vs the new state, and execute additional logic if certain pieces have changed. Examples include , and which offer different approaches to specifying subscriptions and handling changes.

API: Store
#1346: Is it bad practice to just have a 'stores' directory?
Stack Overflow: Redux multiple stores, why not?
Stack Overflow: Accessing Redux state in an action creator
Gist: Breaking out of Redux paradigm to isolate apps
Advanced: Middleware
API: applyMiddleware
#1051: Shortcomings of the current applyMiddleware and composing createStore
Understanding Redux Middleware
Exploring Redux Middleware
redux-watch
redux-subscribe
redux-subscriber
Basics: Store
API: Store
#303: subscribe API with state as an argument
#580: Is it possible to get action and state in store.subscribe?
#922: Proposal: add subscribe to middleware API
#1057: subscribe listener can get action param?
#1300: Redux is great but major feature is missing
Redux Addons Catalog: Store Change Subscriptions
React Redux
Can or should I create multiple stores? Can I import my store directly, and use it in components myself?
Is it OK to have more than one middleware chain in my store enhancer? What is the difference between next and dispatch in a middleware function?
How do I subscribe to only a portion of the state? Can I get the dispatched action as part of the subscription?