Next.js Discord

Discord Forum

UseContext gives a weird Bug

Answered
muadpn posted this in #help-forum
Open in Discord
"use client";
import { WeatherContext } from "@/context/WeatherDataProvider";
import { useContext } from "react";
 const WeatherWidget = () => {
  const { weatherData } = useContext(WeatherContext);
  ....
  works Fine.

"use client";
import { WeatherContext } from "@/context/WeatherDataProvider";
import { useContext } from "react";

const ToggleTemp = () => {
  const { weatherData } = useContext(WeatherContext);

Error:-
react__WEBPACK_IMPORTED_MODULE_2__.useContext(...) is undefined
Answered by Clown
If it is you dont use the curly brackets when importing it
View full answer

19 Replies

Check if WeatherContext is being default exported.
If it is you dont use the curly brackets when importing it
Answer
export default function WeatherDataProvider
You shouldn't be using {} then
its working on the first file ?
How about you try it first
i removed now both are not working
i made the first one with {} and the second one like you said now both are working
Default exports are not supposed to have {} around the import
now its Error: Element type is invalid. Received a promise that resolves to: undefined. Lazy element type must resolve to a class or function.
Thanks.
its me being dumb
🙂
:galactic_brain:
ahhaha
I was just gonna say "either your dev environment haunted or skill issue"
yeaa!! i wrapped the context just a element before and it wasnt able to reach. and the other part i have no idea. i use export default and still the {} this was working fine. i have no idea
Welp, if its solved, mark the answer