I was creating an instagram clone from a youtube tutorial which was posted 1 year ago. There are many errors due to nextjs14, I've solved everyone until this error: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Check the render method of RootLayout.
The code for GlobalContext.js is
import { globalReducer } from "../reducers/globalReducer";
const initialState = {
user:{},
isAuthenticated: false,
isOnboarded: false,
isLoading: false,
}
export const GlobalContext = createContext(initialState);
export const GlobalDispatchContext = createContext(null);
const GlobalContextProvider = ({ children }) => {
const [state, dispatch] = useReducer(globalReducer, initialState)
return (
<GlobalContext.Provider value={state}>
<GlobalDispatchContext.Provider value={dispatch}>
{children}
</GlobalDispatchContext.Provider>
</GlobalContext.Provider>
);
};
export default GlobalContextProvider;
The code for layout.js is
import "./globals.css";
import GlobalContextProvider from "./state/context/GlobalContext";
export default function RootLayout( Component, pageProps ) {
return (
<GlobalContextProvider>
<Component {...pageProps} />
</GlobalContextProvider>
);
}
All the imports and exports are correct as I have checked. Any solution?
I solved that layout.js was not CSR, but after solving it, it showed this error.
The first props of
layout.jsis React children and the second is params.So your
layout.jsfunction should be like this:Now, you're using:
Of course you'll get error. Because
Componentis not a validReact component. Remember, the first props oflayout.jsisreact childrennot a component.Check out the official docs for more info