📝Injecting smart components / containers


Building pages with Storybook (webarchive)

It’s hard to scale Smart/dumb components (Container/View) pattern up to a page level because you can’t nest smart components inside presentational ones. This is especially challenging when parts of the page fetch data independently.

One of the options is to inject smart components (e.g., via context), so they can use real version in production and a mocked “smart” component for testing.


// ProfilePage view component
import { useContext } from 'react';

import ProfilePageContext from './ProfilePageContext';

export const ProfilePage = ({ name, userId }) => {
  const { UserPostsContainer, UserFriendsContainer } = useContext(ProfilePageContext);

  return (
      <UserPostsContainer userId={userId} />
      <UserFriendsContainer userId={userId} />

Then you can use real implementation of containers in application, and mocked implementations for testing.

See also