{"id":15311836,"url":"https://github.com/aslemammad/contextism","last_synced_at":"2026-03-05T09:32:17.717Z","repository":{"id":39040934,"uuid":"282650639","full_name":"Aslemammad/contextism","owner":"Aslemammad","description":"😍 Use React Context better.","archived":false,"fork":false,"pushed_at":"2022-12-13T09:35:36.000Z","size":3516,"stargazers_count":142,"open_issues_count":23,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-29T21:51:51.347Z","etag":null,"topics":["context","react","reactjs","state","state-management"],"latest_commit_sha":null,"homepage":"https://opencollective.com/contextism","language":"TypeScript","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/Aslemammad.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}},"created_at":"2020-07-26T12:59:59.000Z","updated_at":"2024-07-28T03:35:20.000Z","dependencies_parsed_at":"2023-01-28T10:31:14.963Z","dependency_job_id":null,"html_url":"https://github.com/Aslemammad/contextism","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aslemammad%2Fcontextism","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aslemammad%2Fcontextism/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aslemammad%2Fcontextism/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aslemammad%2Fcontextism/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Aslemammad","download_url":"https://codeload.github.com/Aslemammad/contextism/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230423563,"owners_count":18223435,"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":["context","react","reactjs","state","state-management"],"created_at":"2024-10-01T08:34:43.723Z","updated_at":"2026-03-05T09:32:17.664Z","avatar_url":"https://github.com/Aslemammad.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\n    \u003cb\u003eContextism\u003c/b\u003e 🤩 is a new way to use React Context better.\n    \u003cbr\u003e\n    \u003cbr\u003e\n    \u003cimg width=\"230\" height=\"230\" alt=\"picker\" src=\"https://github.com/Aslemammad/Contextism/blob/master/logo.png?raw=true\"\u003e\n    \u003cbr\u003e\n    \u003cbr\u003e\n    \u003ci\u003eRead\n        \u003ca title=\"Team email, team chat, team tasks, one app\" href=\"https://kentcdodds.com/blog/how-to-use-react-context-effectively\"\u003ethis\u003c/a\u003e\n\t    article to become familiar with the idea.\n    \u003c/i\u003e\n    \u003cbr\u003e\n    \u003cbr\u003e\n    \u003cimg src=\"https://img.shields.io/npm/l/contextism\" /\u003e\n\u003c/p\u003e\n\n## Installation 🔥\n\n```bash\nnpm i contextism\n// or \nyarn add contextism\n```\n\n## Usage ✏️\nWe have two ways to use Contextism, Creating store using it or using its hooks directly:\n\n### #1 createStore ✋\n\n```js\n// store.js\nimport { createStore } from \"contextism\"\nconst context = createStore(\"default value for state\")\nexport const {\n    Provider,\n    useDispatchContext,\n    useStateContext,\n    useStore,\n} = context\n\n// App.jsx\nimport Div from \"./Div\"\nimport { Provider } from \"./store\"\n\nconst App = () =\u003e {\n    const [state, dispatch] = React.useState(\"Value for state\") // or useReducer\n\n    return (\n        \u003cProvider state={state} dispatch={dispatch}\u003e\n            // Components you want to use the state there.\n            \u003cDiv /\u003e\n        \u003c/Provider\u003e\n    )\n}\n\n// Div.jsx\nimport { useStateContext, useDispatchContext, useStore } from \"./store\"\n\nconst Div = () =\u003e {\n    const state = useStateContext() // \"Value for state\"\n    const dispatch = useDispatchContext() // dispatch function (setState) in App\n    // or better one\n    const [state, dispatch] = useStore()\n\n    //return ...\n}\n```\n\nWhen we create store using Contextism, it gives us 3 hooks :\u003cbr\u003e\n\n- **useStateContext**: the state value that we gave it to state prop in Provider component\n- **useDispatchContext**: the setState function or useReducer dispatch that we passed it to dispatch prop\n- **useStore**: returns us an array with two values of the above hooks; `[ useStateContext, useDispatchContext ]`\n\n  ***NOTE***: you should use these hooks( methods of createStore function) in child components of *Provider* component.\n  \n### #2 default hooks ✋\nContextism has two hooks beside createStore function:\n\n- **useContext**: takes a React context and returns the value\n- **useStore**: takes two React contexts and returns two values of them, the same thing like in the above way but with two arguments\n\n\n```js\n// Store.jsx\nexport const CountStateContext = React.createContext()\nexport const CountDispatchContext = React.createContext()\nfunction countReducer(state, action) {\n    //...\n}\n\nexport function CountProvider({ children }) {\n    const [state, dispatch] = React.useReducer(countReducer, { count: 0 })\n    return (\n        \u003cCountStateContext.Provider value={state}\u003e\n            \u003cCountDispatchContext.Provider value={dispatch}\u003e\n                {children}\n            \u003c/CountDispatchContext.Provider\u003e\n        \u003c/CountStateContext.Provider\u003e\n    )\n}\n// App.jsx\nimport { CountProvider } from \"./Store\"\nimport Div from \"./Div\"\nexport function App() {\n    return (\n        \u003cCountProvider\u003e\n            \u003cDiv /\u003e\n        \u003c/CountProvider\u003e\n    )\n}\n// Div.jsx\nimport { CountStateContext, CountDispatchContext } from \"./Store\"\nimport { useContext, useStore } from \"contextism\"\n\nexport function Div() {\n    const state = useContext(CountStateContext)\n    const dispatch = useContext(CountDispatchContext)\n    // Or much better:\n    const [state, dispatch] = useStore(CountStateContext, CountDispatchContext)\n\n    //return ...\n}\n```\n\n\n## Typescript 🔷\nContextism has Typescript support like generics and ... . in **createStore** you can pass two generics too, first one for the state structure and interface, the second one for the useReducer hook.\n\n\n```ts\ntype Action = { type: \"increment\" } | { type: \"decrement\" }\ntype State = { count: number }\n// The second generic is for useReducer Action\nconst context = createStore\u003cState, Action\u003e()\n\n// For useState just pass the first generic (State interface generic)\nconst context = createStore\u003cState\u003e()\n```\n\n## Donation\nYou can support me and my projects with [Open Collective](https://opencollective.com/contextism).\n\n## Contribution \nI'm developer, not a perfect person, so I make many mistakes, it means that be free to create issues and then PRs.\n\n## Thanks ❤️ \n\nSpecial thanks for contributing and using this project.\n","funding_links":["https://opencollective.com/contextism"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faslemammad%2Fcontextism","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faslemammad%2Fcontextism","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faslemammad%2Fcontextism/lists"}