{"id":13610439,"url":"https://github.com/xat/react-component-composition-cheatsheet","last_synced_at":"2025-10-30T10:24:55.338Z","repository":{"id":145415939,"uuid":"85483074","full_name":"xat/react-component-composition-cheatsheet","owner":"xat","description":"React component composition cheatsheet","archived":false,"fork":false,"pushed_at":"2017-03-20T02:27:11.000Z","size":1,"stargazers_count":185,"open_issues_count":2,"forks_count":7,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-12-17T01:34:05.245Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-19T14:38:31.000Z","updated_at":"2023-10-08T10:17:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"dc447215-8236-4850-97ba-216408369e10","html_url":"https://github.com/xat/react-component-composition-cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xat%2Freact-component-composition-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xat%2Freact-component-composition-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xat%2Freact-component-composition-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xat%2Freact-component-composition-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xat","download_url":"https://codeload.github.com/xat/react-component-composition-cheatsheet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248642082,"owners_count":21138327,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T19:01:44.725Z","updated_at":"2025-10-30T10:24:50.308Z","avatar_url":"https://github.com/xat.png","language":null,"funding_links":[],"categories":["Others","Cheatsheet"],"sub_categories":["React Components"],"readme":"# React component composition cheatsheet\n\nThis is a small list of common techniques used in React\nto compose and enhance components. Feel free to create\nPull Requests with further techniques or fixes.\n\n### Basic containment\n\nElements can be passed as children into components.\n\n```jsx\nconst Layout = ({ children, theme }) =\u003e (\n  \u003cdiv className={`theme-${theme}`}\u003e\n    \u003cmain\u003e{children}\u003c/main\u003e\n  \u003c/div\u003e\n);\n\nconst Content = () =\u003e (\n  \u003cdiv\u003eSome fancy content\u003c/div\u003e\n);\n\nReactDOM.render(\n  \u003cLayout theme='dark'\u003e\n    \u003cContent /\u003e\n  \u003c/Layout\u003e\n, containerEl);\n```\n\n### Containment with multiple slots using children\n\nChildren are not limited to being elements. You can pass basically anything,\nincluding plain objects.\n\n```jsx\nconst Layout = ({ children, theme }) =\u003e (\n  \u003cdiv className={`theme-${theme}`}\u003e\n    \u003cheader\u003e{children.header}\u003c/header\u003e\n    \u003cmain\u003e{children.content}\u003c/main\u003e\n    \u003cfooter\u003e{children.footer}\u003c/footer\u003e\n  \u003c/div\u003e\n);\n\nconst Header = () =\u003e (\n  \u003ch5\u003eHeader title\u003c/h5\u003e\n);\n\nconst Footer = () =\u003e (\n  \u003cem\u003efooter note\u003c/em\u003e\n);\n\nconst Content = () =\u003e (\n  \u003cdiv\u003eSome fancy content\u003c/div\u003e\n);\n\nReactDOM.render(\n  \u003cLayout theme='dark'\u003e\n    {{\n      header: \u003cHeader /\u003e,\n      content: \u003cContent /\u003e,\n      footer: \u003cFooter /\u003e\n    }}\n  \u003c/Layout\u003e\n, containerEl);\n```\n\n### Containment with multiple slots using props\n\nElements can also be passed in using props.\n\n```jsx\n\nconst Layout = ({ header, content, footer, theme }) =\u003e (\n  \u003cdiv className={`theme-${theme}`}\u003e\n    \u003cheader\u003e{header}\u003c/header\u003e\n    \u003cmain\u003e{content}\u003c/main\u003e\n    \u003cfooter\u003e{footer}\u003c/footer\u003e\n  \u003c/div\u003e\n);\n\nconst Header = () =\u003e (\n  \u003ch5\u003eHeader title\u003c/h5\u003e\n);\n\nconst Footer = () =\u003e (\n  \u003cem\u003efooter note\u003c/em\u003e\n);\n\nconst Content = () =\u003e (\n  \u003cdiv\u003eSome fancy content\u003c/div\u003e\n);\n\nReactDOM.render(\n  \u003cLayout\n    theme='dark'\n    header={\u003cHeader /\u003e}\n    content={\u003cContent /\u003e}\n    footer={\u003cFooter /\u003e}\n  /\u003e\n, containerEl);\n```\n\n### Enhancing contained elements\n\nIt's possible to enhance certain elements with additional props using\n`React.cloneElement`.\n\n```jsx\nconst Layout = ({ children, theme }) =\u003e (\n  \u003cdiv className={`theme-${theme}`}\u003e\n    \u003cmain\u003e{React.cloneElement(children, { theme })}\u003c/main\u003e\n  \u003c/div\u003e\n);\n\nconst Content = ({ theme }) =\u003e (\n  \u003cdiv\u003eCurrently using this theme: {theme}\u003c/div\u003e\n);\n\nReactDOM.render(\n  \u003cLayout theme='dark'\u003e\n    \u003cContent /\u003e\n  \u003c/Layout\u003e\n, containerEl);\n```\n\n### Passing components as props\n\nLike elements, you can also pass components in using props.\n\n```jsx\nconst Layout = ({ children, contentComponent, theme }) =\u003e {\n  const ContentComponent = contentComponent;\n  return (\n    \u003cdiv className={`theme-${theme}`}\u003e\n      \u003cmain\u003e\u003cContentComponent theme={theme} /\u003e\u003c/main\u003e\n    \u003c/div\u003e\n  );\n};\n\nconst Content = ({ theme }) =\u003e (\n  \u003cdiv\u003eCurrently using this theme: {theme}\u003c/div\u003e\n);\n\nReactDOM.render(\n  \u003cLayout\n    theme='dark'\n    contentComponent={Content}\n  /\u003e\n, containerEl);\n```\n\n### Higher Order Components (HOC)\n\nHigher Order Components are functions which get a component passed in as argument and\nreturn a new component.\n\n```jsx\nfunction createComponentWithDefaultProps(WrappedComponent, defaultProps) {\n  return (props) =\u003e (\n      \u003cWrappedComponent {...defaultProps} {...props} /\u003e\n  );\n}\n\nconst Layout = ({ children, theme }) =\u003e (\n  \u003cdiv className={`theme-${theme}`}\u003e\n    \u003cmain\u003e{children}\u003c/main\u003e\n  \u003c/div\u003e\n);\n\nconst DarkLayout = createComponentWithDefaultProps(Layout, { theme: 'dark' });\n\nReactDOM.render(\n  \u003cDarkLayout\u003eSome content\u003c/DarkLayout\u003e\n, containerEl);\n```\n\n### Using functions as children\n\nSince children can be anything, they can also be functions.\n\n```jsx\nclass FetchTheme extends React.Component {\n\n  constructor() {\n    super();\n    this.state = {\n      theme: null\n    };\n  }\n\n  componentDidMount() {\n    fetch('/api/currentTheme')\n      .then((res) =\u003e res.text())\n      .then(((theme) =\u003e {\n        this.setState({ theme });\n      }));\n  }\n\n  render() {\n    const { children } = this.props;\n    const { theme } = this.state;\n    return theme ? children(theme) : null;\n  }\n}\n\nconst Layout = ({ children, theme }) =\u003e (\n  \u003cdiv className={`theme-${theme}`}\u003e\n    \u003cmain\u003e{children}\u003c/main\u003e\n  \u003c/div\u003e\n);\n\nReactDOM.render(\n  \u003cFetchTheme\u003e\n  {\n    (theme) =\u003e (\n      \u003cLayout theme={theme}\u003eSome content\u003c/Layout\u003e\n    )\n  }\n  \u003c/FetchTheme\u003e\n, containerEl);\n```\n\n## License\nCopyright (c) 2017 Simon Kusterer\nLicensed under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxat%2Freact-component-composition-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxat%2Freact-component-composition-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxat%2Freact-component-composition-cheatsheet/lists"}