Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React / Redux: Should containers have any knowledge of state structure?

This tutorial by Dan Abramov suggests that the advantage to using selectors that act on global state (rather than a slice of state) is that they allow containers to be decoupled from knowledge of the state structure.

If that's the case, shouldn't we also avoid directly mapping state values to props, and use selectors instead? Otherwise, our containers must still know about where those values exist in the state tree.

To illustrate using an example...

Directly maps a nested state value to a prop:

const mapStateToProps = (state) => ({
  isModalVisible: state.modal.isVisible,
});

VS

Has no knowledge of state structure. Gets value using isModalVisible() selector:

const mapStateToProps = (state) => ({
  isModalVisible: isModalVisible(state),
});

However, the problem with the latter approach is that for each value in the state tree, we have to write a selector. This seems like a lot of boilerplate code just to select a simple value. Is this considered best practice?

like image 727
pjivers Avatar asked Jan 04 '17 23:01

pjivers


People also ask

Are container components aware of Redux?

Basically, container components are called smart components because each container component is connected to Redux and consumes the global data coming from the store. For example, we might have a container called BlogList connected to Redux, and the source code may look like this.

Why Redux is predictable state container?

Redux is a predictable state container designed to help you write JavaScript apps that behave consistently across client, server, and native environments, and are easy to test. While it's mostly used as a state management tool with React, you can use it with any other JavaScript framework or library.

Should all state be stored in Redux?

Yes, it's worth striving to store all component state in Redux. If you do, you will benefit from many features of Redux like time travel debugging and replayable bug reports. If you don't, those features could be completely unusable.

What are the mandatory attributes of Redux action?

The official documentation only states that a Redux action has to be a plain object and needs a string action type: A plain object describing the change that makes sense for your application. ... Actions must have a type field that indicates the type of action being performed.


1 Answers

The answer for your question is: "it depends"

Do you have a tiny app?

Maybe avoid using redux at all and stick to react components state.

      You Might Not Need Redux

Do you have a small app?

Writing a bunch of selectors is a lot of boilerplate just like you said. Avoid writing them and stick to simply mapping, using mapStateToProps.

Medium to large apps?

This is where selectors and memoized selectors pay-off. You'll find yourself checking if modals is visible in many components. Currently in your state modal is under state.modal.visible. But tomorrow a modal might be a part of a parent modal, you'll have to change all mapping in all your components to state.parentModal.modal.visible. You can see how this can turn badly right?

The pros of selectors:

  • Hides complexity when working in a team.
  • Changing your state means only modifying your selector function.
  • Avoid writing filters and reducing lists on each mapPropsToState function.

Pros of Memoized selectors:

  • Performance.

Cons

  • Boilerplate code and slower to get started.
  • Additional libraries.

Hope it answers your question.

like image 101
SagiSergeNadir Avatar answered Nov 10 '22 07:11

SagiSergeNadir