Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

useContext() is undefined when i use useLocation()

I'm working on a website using React. I'm getting this error when using useContext() and useLocation().

import React, { useContext } from "react";
import Layout from "../components/Layout";
import { LangStateContext } from "../context/LangContext";

import { useLocation } from 'react-router';


const PricingPage = () => {
  const { isIndoSelected } = useContext(LangStateContext);
  console.log(isIndoSelected);

  const location = useLocation();


  return (
    <Layout>
      a page
    </Layout>
  );
};

export default PricingPage;

This is the error im getting when i land on the page

useContext is undefined error

Can anyone explain to me why this wont work and what a good workaround would be?

Thanks you

like image 977
Sil Avatar asked May 10 '26 06:05

Sil


1 Answers

useContext needs Providers to work, so make sure that you have instantiated one that has this component as a direct or indirect child.

Also make sure that the context that you have created to instantiate this provider has this isIndoSelected that you're trying to access.

You can read more about React's Context usage here.

like image 105
Daniel Baldi Avatar answered May 12 '26 19:05

Daniel Baldi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!