useContext is a React hook for managing global state through the Context API.
Questions tagged [use-context]
586 questions
0
votes
1 answer
Typescript type for `React.createContext(null)` when using null for initial value
I have this hook that I use to get the value from a useContext
useFirebase.js
import React, { useContext } from 'react';
export const FirebaseContext = React.createContext(null);
function useFirebase(): firebase.app.App {
const firebase =…

cbdeveloper
- 27,898
- 37
- 155
- 336
0
votes
1 answer
useContext returning an empty object in Gatsby application
When using useContext() I'm getting an empty object.
I created a file that contains my Context, Provider, and Consumer
src/utils/notesContext.js
import PropTypes from "prop-types"
import React, { createContext, useState } from "react"
export const…

alberto_g
- 39
- 10
0
votes
1 answer
In react useReducer store loses state when I route to another page. useContext loads only initial state
State is saved well on page and gets changed and interactive, but once I navigate to another page - state is reset
The whole store and createContext is
type Action2 =
{
type: "ON";
payload: boolean;
}
| {
type: "OFF";
…

3cirlces
- 105
- 11
0
votes
1 answer
Why are is the info I'm getting from my backend getting console.logged but not displayed in React?
My app consists of the following: It's an app using the MERN stack and the goal is to be a able for users to login, register and view contacts. (It's just for practice, I have no plans on deploying it).
The problem occurs when the User logs in, and…

gxp2
- 149
- 2
- 11
0
votes
1 answer
In React, global state goes out of sync when route parameters are manually changed in url
We have a variable in our React application that is both:
Defined as global state in App.js, passed globally with its setter method to other components via the GlobalContext.Provider, and
Used separately as a route parameter for many of the app's…

Canovice
- 9,012
- 22
- 93
- 211
0
votes
1 answer
Theme Provider issue
I'm trying to set up a theme provider for a little project. Each time the user click on a button the app theme (color) is supposed to change. Let's say I've got four themes in a separate file like this:
const themesArray = [
{ primaryColor:…

chalatum
- 67
- 1
- 3
- 9
0
votes
1 answer
Function from useState is usable if using props drilling but not from useContext
Good morning,
I am converting an application over from props drilling to useContext and am encountering a strange issue. A function I have assigned to a prop is seen as a function as a drilled prop but not when pulled out of a created context. I…

J-Daniel-S
- 47
- 8
0
votes
0 answers
React TypeError: dispatch is not a function
Please help me I am completely messed with my code and I am getting this error and I am unable to resolve it.
I know there are several answers to this question but they didn’t resolve my problem. I am new to react and am unable to find the correct…

SHUBHAM KUMAR
- 31
- 2
0
votes
0 answers
UseContext not updating all the components
TestContext.js
import { createContext } from 'react';
const CheckoutDrawerContext = createContext({
showDrawer: false,
toggleCheckoutDrawer: () => {},
});
export default CheckoutDrawerContext;
TestComponent.jsx
export default function…

Explain Down Vote
- 151
- 6
- 22
0
votes
0 answers
React hooks useContext : Uncaught (in promise) TypeError: setUser is not a function
hey guys I'm currently trying to use hooks and useContext to do authentication
I defined useContext in this file:
import { createContext } from "react";
export const UserContext = createContext(null);
in my Router file I have setup useContext in…

richie4k2k
- 111
- 1
- 1
- 7
0
votes
0 answers
Reset React contexts after use
In my project, there are several elements rendered in the page. I've been using useContext to update certain elements when changes occur. For example, when the user edits a role with the EditRole component, the updatedRole context is set to that…

Capeudinho
- 33
- 1
- 6
0
votes
1 answer
Modifying object inside array with useContext
I've been having trouble using React's useContext hook. I'm trying to update a state I got from my context, but I can't figure out how. I manage to change the object's property value I wanted to but I end up adding another object everytime I run…

Matheus Ciappina Pereira
- 33
- 2
- 7
0
votes
1 answer
How to prevent infinite loop with React's useReducer, useContext and useEffect
I'm currently trying to figure out how to avoid creating an infinite loop when wrapping my application in a Context provider (taking in values from useReducer) and then updating via child component with a useEffect hook.
There's an example of the…

thathurtabit
- 558
- 2
- 11
- 22
0
votes
2 answers
useContext not updating on the same render
I have got an issue where I need to update the name in my useContext and then straight away check it against another part of my code. Although when I check it against another part the name value in the context is still empty.
So this is my context…

Shaun Evans
- 23
- 4
0
votes
1 answer
Testing react component that is dependent on Stateful Context Providers chain in React Testing Library
I'm aware of this, but it's not my case.
Example:
All of these providers are actually regular React Components with state and effects, that…

cule111
- 49
- 1
- 9