useContext is a React hook for managing global state through the Context API.
Questions tagged [use-context]
586 questions
0
votes
1 answer
React Testing Library with UseReducer & UseContext not updating state properly
I created a sandbox to outline the main points of interest:
https://codesandbox.io/s/restless-dawn-nwy0l
Please ignore the formatting as this is just a MWE I put together.
When I run the following test in the sandbox above
import React from…

lbragile
- 7,549
- 3
- 27
- 64
0
votes
2 answers
How can I reach the variable which i named "global" from GlobalContext?
I have a GlobalContext.js file. I want to reach this part by having a variable named "global" in my MainScreen. But it doesn't see it. Where could be my mistake?
I thought what I did was correct, but it doesnt work
Here is GlobalContext.js
import…
user15974221
0
votes
1 answer
useContext returns undefined when my redux store updates
I am having trouble understanding why my useContext value updates from the expected values to undefined when the redux store is updated. I am using create context to house my socket connection for my discussions. The following is my context…

OmarSaid
- 37
- 7
0
votes
0 answers
react useContext, when I remove an element, another element get removed instead
I'm coding a simple todo app using hooks,
Its functions are(add task, remove task, edit task)... basic stuff
Everything seems to work okay except for the delete button. when I click delete on any element, the last element gets deleted everytime.
the…

Fayez Als
- 1
- 2
0
votes
2 answers
How can I manage 2 different states in my createContext() and createContext().Provider?
I have 2 states, one is const [users, setUsers], other one is const [horizontalDiscussion, setHorizontalDiscussion]
users, setUsers parts are working, but not the horizontalDiscussion, and setHorizontalDiscussion.
May be cause of doing it in the…
user15974221
0
votes
1 answer
How do you change the current path name in real time based on textbox value? react router
Suppose there is a route like /search
the route is defined like
import { Link, BrowserRouter as Router, Route } from "react-router-dom";
import { useHistory, useLocation } from "react-router-dom";
...
...

Ridhwaan Shakeel
- 981
- 1
- 20
- 39
0
votes
2 answers
What do I do wrong when I use createContext(), and useContext()?
I can do this without using createContext(), and useContext(). But I want to learn to use this, so I want to add useContext, and createContext.
Normaly i have this code in my MainScreen.js
const [users, setUsers] = useState([
…
user15974221
0
votes
1 answer
JSON Parse error: Unexpected identifier "undefined" with localStorage
I'm been using useContext to get my variables from one page to the other and I implemented localStorage so I refresh the page. However, when I refresh my portals
page I'm getting this error:
JSON Parse error: Unexpected identifier "undefined"
The…

Rafael
- 191
- 1
- 11
0
votes
2 answers
Objects are not valid as a React child (found: [object Promise]) while rendering Firebase results
I am getting the following error:
Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
I am using context in react and having a problem with props as I have…

sourav ojha
- 5
- 3
0
votes
0 answers
useContext returning null within child component, not parent component?
I have a parent component that can use useContext() to return the context, but if it's child component uses useContext() it returns the initial context (null in this case) and not the actual context.
I have no idea why this is happening because the…

Noob
- 754
- 3
- 10
- 27
0
votes
1 answer
Passing a state with context in react
I'm learning react and using context for the first time. I'm trying to pass a state and setState function through context to all children components, and I keep getting an error.
This is how I'm creating the context:
import {createContext} from…

German Cocca
- 669
- 1
- 9
- 17
0
votes
1 answer
Passing value by using Context API and useContext (React js)
I want to pass value of customerFlag which i have declared in Login.jsx component and want to access that value in Header.jsx. I am using Context API and useContext. Basically here i am passing value between two components you can say sibling…

Muhammad Usman
- 49
- 1
- 9
0
votes
1 answer
Sort data in Axios response and set as useReducer payload
I'm calling data from an api into my react app using axios, like so:
const adapter = axios.create({
baseURL: "http://localhost:4000",
});
const getData = async () => {
const response = await adapter.get("/test-api");
return…

Jesse Winton
- 568
- 10
- 33
0
votes
0 answers
How to make data accessible to subchild component using React useContext
I'm trying to build React app that shows a ProfilePage component only if user Logged in. Ive Application component(wrapped in Provider component called UserProvider) which renders either ProfilePage or Route to either Signin or signup pages…

charishma
- 3
- 4
0
votes
1 answer
Is there any way to combine useContext with an arrow function?
I'd like to use the useContext and createContext with an arrow function. I want to be able to use variables inside Foo and pass them in createContext.
export const Foo = () => {
...
}
Combine above and below.
export const Bar =…

dannym
- 95
- 2
- 10