Skip to content

React Elements in Redux state (docs/discussion) #1793

@guigrpa

Description

@guigrpa

The Redux docs recommend not putting anything non-serializable in store state, and link to related discussions about not putting React components in state (#1248) but rather identifiers of components we want rendered (#1390). What is not explicitly covered is the possibility to include React elements in state.

@gaearon explains React Elements very clearly as "plain object describing a component instance or DOM node and its desired properties" (see here and here). Conceptually, it doesn't look like a great "heresy" to put Elements in state if the need arises.

In my particular case, I have this need. I have Component Foo that wants to render a dropdown palette (Palette) but can't do it himself because of some CSS constraints (it is inside a div with transform: translateZ(0) for performance, and hence the dropdown is truncated when using position: fixed). One way to solve this is to have Foo send a description of Palette to another component (call it Floats) higher up the render tree. Floats is not constrained by that transform CSS and can position its floating children freely. It could even receive Elements from multiple React Components and centrally manage them, keeping these Elements (why not?) in a Redux store. The store could handle actions for creation, update, removal of those floats.

My question about the orthodoxy of this Elements-in-state idea on StackOverflow. was answered with a clear NO and even down-voted (!!), but no alternative way was suggested. I finally ended up doing it, since (a) I don't need to persist/re-hydrate this state; and (b) it doesn't break time travel (as stated here; I built a fiddle to show this, but Redux DevTools seem to break in JsFiddle).

Maybe we could discuss (and add our conclusions to the docs (#857)) whether this solution is bad, in which scenarios it may be OK, or even if there is a more orthodox way for transferring Elements (not Component descriptors) through state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions