The Flux pattern of having React components listen for updates to a central store is great, but seems to present an issue when you have numerous small, composable parts all on the same page requiring re-renders from a single store. On first inspection, it looks like the developer is required to tradeoff between two choices: creating a parent component which manages state and the rendering responsibilities of these smaller, repeatable components vs having each component manage its own resources, but hold a large number event handlers open (one for each repeated component).
To illustrate, my existing structure looks something like this:
- I have an
EventList
component that contains a list of (potentially hundreds of)Event
components to render as children. - The
Event
component includes anAuthenticatedImage
as a child component when rendering. - The
AuthenticatedImage
component listens to changes on theTokenStore
and re-renders when an update occurs (a new token, re-render with that token).
TokenStore
updates very rarely, but we definitely want to re-render all AuthenticatedImage
components when this happens.
Here's the dilemma: If I have each AuthenticatedImage
listen for changes to the TokenStore
, Javascript starts complaining that we have a large number of open event handlers to the same event (that is, potentially hundreds of components all listening to the same event). In contrast, I could have the parent EventList
component listen for updates to TokenStore
, but then EventList
starts owning the responsibilities of AuthenticatedImage
, and AuthenticatedImage
loses its portability.
Given these thoughts, what is the correct way to ensure that numerous instances of a component are re-rendered on a store change, without consuming exorbitant amounts of memory and angering the Javascript Gods, while ensuring code is kept clean and portable?