{"id":1263,"date":"2025-05-22T12:14:14","date_gmt":"2025-05-22T12:14:14","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=1263"},"modified":"2026-02-05T12:06:04","modified_gmt":"2026-02-05T12:06:04","slug":"context-api-pitfall-in-react","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/context-api-pitfall-in-react\/","title":{"rendered":"What is the Context API pitfall and how do you avoid it?"},"content":{"rendered":"\n<p>The main pitfall is that all components consuming a context via , even if the specific data they use hasn&#8217;t changed. This happens because the provider usually passes a new object reference on each render.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are the Avoidance Strategies of Context API pitfall?<\/h2>\n\n\n\n<p><strong>Split Contexts:<\/strong> Create smaller, more focused contexts instead of one large global context. Components subscribe only to the context they need.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Memoize the Context Value:<\/h3>\n\n\n\n<p>Wrap the value object passed to the Provider in useMemo (and memoize functions within it using useCallback) so its reference only changes when its actual contents change.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use Libraries with Selectors:<\/h3>\n\n\n\n<p>State management libraries (Zustand, Jotai) or use-context-selector allow components to subscribe only to specific slices of the context state.<\/p>\n\n\n\n<p><strong>Problem:<\/strong> Every consuming component re-renders when any value in context changes.<\/p>\n\n\n\n<p><strong>Fix:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Split contexts (AuthContext, ThemeContext)<\/li>\n\n\n\n<li>Use memoization:<\/li>\n<\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ AuthProvider.js\nconst AuthContext = createContext(null);\nexport const AuthProvider = ({ children }) => {\n  const &#91;user, setUser] = useState(null);\n  const logout = () => { \/* logout logic *\/ };\n  const value = useMemo(() => ({ user, logout }), &#91;user]);\n  return (\n    &lt;AuthContext.Provider value={value}>\n      {children}\n    &lt;\/AuthContext.Provider>\n  );\n};\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The main pitfall is that all components consuming a context via , even if the specific data they use hasn&#8217;t changed. This happens because the provider usually passes a new object reference on each render. What are the Avoidance Strategies of Context API pitfall? Split Contexts: Create smaller, more focused contexts instead of one large [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1268,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[6,3],"tags":[],"class_list":["post-1263","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=1263"}],"version-history":[{"count":6,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1263\/revisions"}],"predecessor-version":[{"id":1271,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1263\/revisions\/1271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/1268"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=1263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=1263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=1263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}