ReduxKotlin

ReduxKotlin

  • Getting Started
  • API
  • FAQ
  • Github
  • Need help?

›FAQ

Introduction

  • Getting Started with Redux
  • Motivation
  • Core Concepts
  • Three Principles
  • Threading
  • Learning Resources
  • Ecosystem
  • Examples

Basic Tutorial

  • Basic Tutorial: Intro
  • Actions
  • Reducers
  • Store
  • Data flow

Advanced Tutorial

  • Advanced Tutorial: Intro
  • Async Actions
  • Middleware

FAQ

  • FAQ Index
  • General
  • Reducers
  • Store Setup
  • Multiplatform

Other

  • Glossary
  • Troubleshooting
  • Feedback

API Reference

  • API Reference
  • createStore
  • createThreadSafeStore
  • createSameThreadEnforcedStore
  • Store
  • applyMiddleware
  • compose

Redux FAQ: Reducers

Table of Contents

  • How do I share state between two reducers? Do I have to use combineReducers?
  • Do I have to use the switch statement to handle actions?

Reducers

How do I share state between two reducers? Can I use combineReducers?

The suggested structure for a Redux store is to split the state object into multiple “slices” or “domains” by key, and provide a separate reducer function to manage each individual data slice. This results in smaller, manageable reducer functions.

Many users later want to try to share data between two reducers, but find that they do not have access to the needed substate in a particular reducer. There are several approaches that can be used:

combineReducers is a commonly used function in JS Redux that aids in reducer composition. In ReduxKotlin there is a combineReducers function, but is more limited. It only allows combining reducers of the same state type.

  • If a reducer needs to know data from another slice of state, the state tree shape may need to be reorganized so that a single reducer is handling more of the data.
  • Handle the action in the root reducer

In general, remember that reducers are just functions—you can organize them and subdivide them any way you want, and you are encouraged to break them down into smaller, reusable functions (“reducer composition”). While you do so, you may pass a custom third argument from a parent reducer if a child reducer needs additional data to calculate its next state. You just need to make sure that together they follow the basic rules of reducers: (state, action) -> newState, and update state immutably rather than mutating it directly.

Further information

Documentation

  • Basics: Reducers

Discussions

  • Reddit: Why reducers?

Do I have to use the when statement to handle actions?

No. You are welcome to use any approach you'd like to respond to an action in a reducer. The when statement is a common approach, but it's fine to use if statements, a reducible interface, or to create a function that abstracts this away.

← GeneralNext →
  • Table of Contents
  • Reducers
    • How do I share state between two reducers? Can I use combineReducers?
    • Do I have to use the when statement to handle actions?
ReduxKotlin
Docs
Getting StartedCore ConceptsBasicsAdvanced
Community
#redux slackTrello board
More
GitHubStar
Thank you to Dan Abramov and the Redux.js.org documentation authors from which this was forked.
Some icons copyright Font Awesome and Noun Project (Hassan ali, ProSymbols)