useContext is a React hook for managing global state through the Context API.
Questions tagged [use-context]
586 questions
2
votes
1 answer
How to use useContext data with properties
I'm using useContext in a component page, and it correctly gets datas through useContext in a type of a property.
colorContex.js
import { createContext, useEffect, useState, useContext } from 'react';
// create context object
export const…

Yuzu
- 59
- 1
- 8
2
votes
1 answer
useContext is giving me problems with my react router dom
so I have this app file for react,
const [cart, setCart] = useState([
])
return (
}>
{/* you wrap provider around the…

evan_tech1234
- 123
- 12
2
votes
2 answers
React Context is not working as expected: Unable to change the value of shared variable
I made a context to share the value of the variable "clicked" throughout my nextjs pages, it seems to give no errors but as you can see the variable's value remains FALSE even after the click event. It does not change to TRUE. This is my first time…

sbunn
- 33
- 1
- 6
2
votes
4 answers
How to use useContext inside a component
I have a context that I have defined as 'HeaderContext' - I know its getting my data correctly becuase I can see the data in the console.log(headerData.Menu.PageUrl) line.
However I only see that when I uncomment the line and save the file after the…

Ayo Adesina
- 2,231
- 3
- 37
- 71
2
votes
1 answer
Why does optional chaining allows rendering when fetching data through useEffect in an app that uses context?
I'm new to the webdev world and want to learn ReactJS. I followed a tutorial I found on YouTube made by Traversy where he makes a task tracker and now I want to make some changes to it to learn and practice some more.
I want to use context for the…

miguelC
- 21
- 4
2
votes
1 answer
'dispatch' is not defined when using useReducer with useContext in react
I'm trying to figure out how to change global state from a componenet using useContext and useReducer dispatch method.
The component is simply should change the backgournd of the page on a click
Here is how I defined the context…

MBH
- 109
- 1
- 8
2
votes
1 answer
useContext with no context
I have defined a context using createContext that wraps some of my components like
import MyContext from './mycontext.js'
import A from 'a.js';
import B from b.js';
where A is defined something like
import…

Kevin
- 91
- 1
- 1
- 7
2
votes
0 answers
React state setter fails to set the state of user object in firebase authentication
Hey guys I am trying to use Firebase Authentication in my React App and I am doing this by setting up an context. I have checked a lot of questions regarding this and I wasn't able to get a satisfactory answer and so I am asking this here.…

SANKET SHINDE
- 31
- 3
2
votes
1 answer
reactjs: a context component function not updating its variable; usestate not working immediately?
I'm working on auth in my app, so I created a context to manage it:
import React, { useState } from "react";
const AuthContext = React.createContext({
token: "",
isLoggedIn: false,
login: (token) => { },
logout: () => {…

BogdanB
- 139
- 10
2
votes
0 answers
Can't use useContext in getStaticPaths NextJS Function
Nextjs throws an error: Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
Whenever I try to access useContext Value in getStaticPaths() function.
Here's my code:
export async function getStaticPaths() {
…

Sayuri Kamble
- 21
- 1
2
votes
1 answer
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…

Sil
- 33
- 4
2
votes
2 answers
TypeError: Cannot read property '_context' of undefined
I am creating a simple Todo List using React, Next.js, and TailwindCSS. For some reasons, I got this error: TypeError: Cannot read property '_context' of undefined.
This error occurs in TodoForm.js. I have defined showModal in index.js using…

Uhney
- 379
- 1
- 6
- 23
2
votes
2 answers
Object is undefined when trying to fetch it using useContext()
I am very new to React and was trying to make a context in React so that in my notes app such that I can trigger my custom made alert for relevant user activity but when I am trying to use the values from the context using useContext I am getting…

Tushar Verma
- 163
- 1
- 9
2
votes
1 answer
TS2322: Type error while im using useContext with a function
Im tyring to pass through Provider value ={ ..} the states and functions that i want react to listen on changes.
I have login function that accept userId and token and a logout function that accept nothing.
In the AuthContext i made an interface…

Oz Shurki
- 21
- 2
2
votes
2 answers
How to use useEffect correctly with useContext as a dependency
I'm working on my first React project and I have the following problem.
How I want my code to work:
I add Items into an array accessible by context (context.items)
I want to run a useEffect function in a component, where the context.items are…

Matus86309
- 41
- 1
- 8