You get a bonus - 1 coin for daily activity. Now you have 1 coin

What is a switching component? in REACT

Practice



A switching component is a component that renders one of many components. We need to use an object to map prop values to components.

For example, a switching component that displays different pages based on the

page
prop:
import HomePage from './HomePage'
import AboutPage from './AboutPage'
import ServicesPage from './ServicesPage'
import ContactPage from './ContactPage'

const PAGES = {
  home: HomePage,
  about: AboutPage,
  services: ServicesPage,
  contact: ContactPage
}

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

  return <Handler {...props} />
}
//The keys of the PAGES object can be used in the prop types to catch development-time errors. 

Page.propTypes = {
  page: PropTypes.oneOf(Object.keys(PAGES)).isRequired
}

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Scripting client side JavaScript, jqvery, BackBone"

Terms: Scripting client side JavaScript, jqvery, BackBone