# Component Switch

A switching component is a component that renders one of many components.

Use an object to map prop values to components

```javascript
import HomePage from './HomePage.jsx';
import AboutPage from './AboutPage.jsx';
import UserPage from './UserPage.jsx';
import FourOhFourPage from './FourOhFourPage.jsx';

const PAGES = {
  home: HomePage,
  about: AboutPage,
  user: UserPage
};

const Page = (props) => {
  const Handler = PAGES[props.page] || FourOhFourPage;

  return <Handler {...props} />
};

// The keys of the PAGES object can be used in the prop types to catch dev-time errors.
Page.propTypes = {
  page: PropTypes.oneOf(Object.keys(PAGES)).isRequired
};
```

## Related links:

* [https://hackernoon.com/10-react-mini-patterns-c1da92f068c5](https://hackernoon.com/10-react-mini-patterns-c1da92f068c5)
