A switching component refers to a component that renders one of multiple components dynamically. This is typically done using an object to map prop values to components.
The example below demonstrates how to display different pages using a single switching component based on the page prop.
import HomePage from './HomePage'
import AboutPage from './AboutPage'
import FacilitiesPage from './FacilitiesPage'
import ContactPage from './ContactPage'
import HelpPage from './HelpPage'
const PAGES = {
home: HomePage,
about: AboutPage,
facilitiess: FacilitiesPage,
contact: ContactPage,
help: HelpPage
}
const Page = (props) => {
const Handler = PAGES[props.page] || HelpPage;
return <Handler {...props} />
}
// The PAGES object keys can be used in the prop types for catching errors during dev-time.
Page.propTypes = {
page: PropTypes.oneOf(Object.keys(PAGES)).isRequired
}