Skip to content

RFC: Reuse complex components implemented in React plus Redux #278

@sompylasar

Description

@sompylasar

This post is meant to be a start for a discussion. Moved from here as @gaearon suggested here.

Intro

I'm interested in finding best practices on how to architect complex components implemented in React and Redux so that they are reusable as a whole in another app.

Not sure how widespread is the problem, but I encounter it from time to time. I hope the developers from the front-end community encounter similar problems, too.

Terms and definitions

A complex component -- a UI (React, Redux actions), coupled with business logic (Redux reducer), and data access logic (Redux actions' side effects; middleware).

Traits of a complex component:

  • can be instantiated more than once, maybe simultaneously (not a singleton)
  • each instance can have its own configuration
  • can query and manipulate the global environment:
    • the URL and the history (routing, back-forward)
    • network communication (AJAX, WebSockets etc.)
    • storage (cookie, localStorage, sessionStorage etc.)
    • viewport dimensions, global events like viewport scrolling/resizing
  • can depend on the app state:
    • query and manipulate other components
    • delegate some functionality, e.g. asset loading, full-screen modal container etc.
  • should not pollute the environment
  • when used from another app, the component should be reused, not copy-pasted

An app -- a UI environment where the components are configured and instantiated.

Traits of an app to consider:

  • can be a React + Redux app
  • can be a React-only app
  • can be a non-React app

Examples of components

  • a wizard, a multi-step form, a questionnaire
  • a complex stateful popup, like a multi-tab settings dialog, or a chat
  • a WYSIWYG editor with autocompletion and image uploads

Developing such components with Redux adds the invaluable benefits of predictability and replayability.

Questions to answer

  • How to structure the component code (where to put reducers, actions, UI code)
  • How to put a component into a React + Redux app
  • How to put a component into an app that has no Redux and/or React
  • How to isolate the state of the component instance
  • How to configure the component reducers' logic based on the component instance configuration
  • How to target actions at specific component instances' state
  • How to handle actions of a specific component instance in the app reducers
  • How to bridge the component with the global environment (URL and history, network, storage)
  • How to bridge the component with the app state
  • How to bridge the component with the functionality provided by an app (asset loading, full-screen modal container etc.)

React developers from Facebook answered that I should "start by reusing React components only", but having a lot of business logic copied from app to app is not the best way to go.

Elm architecture answers some of the questions, but Redux is quite different (no view+reducer coupling, no explicit serializable side-effects).

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions