react-bits:组件切换

前端之家收集整理的这篇文章主要介绍了react-bits:组件切换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

react-bits
原文

从多个组件中选一个渲染
使用对象映射props值和组件

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
};

猜你在找的React相关文章