{"id":13781898,"url":"https://github.com/SunHuawei/with-context","last_synced_at":"2025-05-11T15:32:06.401Z","repository":{"id":57398395,"uuid":"123118625","full_name":"SunHuawei/with-context","owner":"SunHuawei","description":"Decorator for new React Context API","archived":false,"fork":false,"pushed_at":"2018-02-28T10:54:37.000Z","size":7,"stargazers_count":53,"open_issues_count":3,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-05T11:12:34.910Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SunHuawei.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-27T11:18:44.000Z","updated_at":"2023-02-06T23:28:51.000Z","dependencies_parsed_at":"2022-08-29T18:51:03.719Z","dependency_job_id":null,"html_url":"https://github.com/SunHuawei/with-context","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/SunHuawei%2Fwith-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SunHuawei%2Fwith-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SunHuawei%2Fwith-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SunHuawei%2Fwith-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SunHuawei","download_url":"https://codeload.github.com/SunHuawei/with-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253588625,"owners_count":21932289,"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-03T18:01:30.520Z","updated_at":"2025-05-11T15:32:06.088Z","avatar_url":"https://github.com/SunHuawei.png","language":"JavaScript","readme":"# with-context\nBest practice of new React Context API\n\n## Why with-context?\n1. Less boilerplate/verbosity\n2. Make the usage more easier\n3. Tiny, only 1.8k before compressed\n\nSuggest considering `with-context` as your best practice.\n\n## Live Demo\nCheck here for online live demo: [https://jqkyy1oyv.codesandbox.io/](https://jqkyy1oyv.codesandbox.io/)\n\n## How to install\n\n```bash\nnpm i --save with-context\n```\n## Simple Usage\n\nYou could use `with-context` as a decorator -- `@withContext(SomeContext)` -- on your leaf components.\n\nHere is a example, you may have a file `withTheme.js`\n\n```jsx\nimport { withContext } from \"with-context\";\n\nexport const ThemeContext = React.createContext(\"light\");\nexport const withTheme = withContext(ThemeContext, \"theme\");\n```\n\nWrap your top component by `ThemeContext` just as the official demo.\n\nAnd then, you could use `withTheme` for any leaf component which need theme.\n\nYou could use it as a decorator on your leaf component `LeafComponent.js`. And then you could simply use `this.props.theme` in that component.\n\n```jsx\nimport { withTheme } from \"./withTheme\";\nimport { styles } from \"../consts\";\n\n@withTheme\nexport default class LeafComponent extends React.PureComponent {\n  render() {\n    const { theme } = this.props;\n    return (\n      \u003cdiv style={styles[theme]}\u003eLeafComponent with theme: {theme}\u003c/div\u003e\n    );\n  }\n}\n```\n\n## Apply multiple context\nYou also could apply multiple context by this API -- `@withMultiContext({theme: ThemeContext, lang: LangContext})`.\n\nHere is a example, you could have a file `withThemeAndI18n.js`\n```jsx\nimport { withMultiContext } from \"with-context\";\n\nexport const ThemeContext = React.createContext(\"light\");\nexport const LangContext = React.createContext(\"en\");\nexport const withThemeAndI18n = withMultiContext({\n  theme: ThemeContext,\n  lang: LangContext\n});\n```\n\nAnd then for a leaf component `LeafComponent.js`, you could use `const { theme, lang } = this.props`.\n\n```jsx\nimport { withThemeAndI18n } from \"./withThemeAndI18n\";\nimport { styles, langs } from \"../consts\";\n\n@withThemeAndI18n\nexport default class LeafComponent extends React.PureComponent {\n  render() {\n    const { theme, lang } = this.props;\n    const langSet = langs[lang];\n    return (\n      \u003cdiv style={styles[theme]}\u003e\n        \u003cp\u003ewith theme: {langSet \u0026\u0026 langSet[theme]}\u003c/p\u003e\n        \u003cp\u003ewith lang: {lang}\u003c/p\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n## Work with stateless functional component\n\n`with-context` also works with stateless functional component. For example.\n\n```jsx\nimport { withTheme } from \"./withTheme\";\nimport { styles } from \"../consts\";\n\nconst StatelessFunctionalComponent = ({ theme }) =\u003e {\n  return (\n    \u003cdiv style={styles[theme]}\u003e\n      StatelessFunctionalComponent with theme: {theme}\n    \u003c/div\u003e\n  );\n};\n\nexport default withTheme(StatelessFunctionalComponent);\n```","funding_links":[],"categories":["Libraries","List"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSunHuawei%2Fwith-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSunHuawei%2Fwith-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSunHuawei%2Fwith-context/lists"}