NextJS & Reactjs: Error createContext) is not a function

very frustrating error coming off from nextjs with server side and client side rendering, essentiality, we are trying to access createContext (an reactjs function) that is a client side function, during server side rendering! the fix is easy. it’s a common beginners mistakes where you assume and not familiar with when and where code should be written or familiar with what is considered backend code and what is considered front end code.

Solution

If your entire component is front end or client side only, all you’ll need to do is to tell the compiler this code/function/component are client side rendering only, this way the component won’t be attempted rendering in the backend and won’t be able to detect createContext (which is, again a front end/client side function).

"use client"
// code here will be rendered in the client side only, where we can find "createContext"

if you component is big and you still wants the benefits of server side rendering, all you need to do is some decoupling, and create another file for the specific area where you use “createContext” (or the 3rd party library that you use) and within the new component, simply add the “use client” at the top, and import that component into the larger/parent/previous component file.
This way you’ll benefits the best of both worlds, the server side rendering for the entire page and the client side rendering for that specific component – Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

All rights reserved 2024 ©