Questions tagged [use-context]

useContext is a React hook for managing global state through the Context API.

313 questions
9
votes
1 answer

React - useContext inside class

I'm newbie in react and i want use useContext inside my class, how do i solve this? This is example of my current code right now import { Context } from '../context/ChatListContext' const ChatList = ({ onAction }) => { const {state,…
Hanif Nr
  • 164
  • 1
  • 10
9
votes
4 answers

Better way to use multiple context providers in ReactNative

I am having an app in which I am using 3 Context Provider. For the app to work, I have to wrap in all these providers. As my app grows I am expecting to have a few more providers for more types of data I have to connect with. I have already…
esafwan
  • 14,622
  • 30
  • 99
  • 154
8
votes
2 answers

Typescript is giving me an error: Expected 0 arguments, but got 1

I want to define the interface the proper way but I am having trouble because it is expecting an argument even though the argument is empty. I am using useContext and I defined the interface like this: //react-auth0-spa.tsx interface…
bradrar
  • 475
  • 1
  • 5
  • 13
6
votes
1 answer

React useContext not passing value down to deeply nested children

I'm brand new to the useContext hook and trying to figure out what I'm doing wrong... without the top imports, here's what my code looks like: So the root of my project looks like this: export default function App() { return (
5
votes
1 answer

React-Native - TypeError: undefined is not an object evaluating useContext

useContext works fine here, the console.log right before the screen change works as expected and gives me the user object (just copy/pasted the relevant info to save you time) import React, { useContext, useState, useEffect } from "react"; import {…
5
votes
0 answers

How to test react useContext useReducer dispatch in component

hope someone can point me the right direction with this. Basically I've created a react app which makes use of hooks, specifically useContext, useEffect and useReducer. My problem is that I can't seem to get tests to detect click or dispatch…
Noelt
  • 71
  • 5
5
votes
2 answers

React Use Context. Cannot destructure property 'currentChatbotInEdit' of 'Object(...)(...)' as it is undefined. How can I fix that?

I try to import the chatbotcontext in order to get access to current chatbot in edit, but something went wrong. Do you have ideas what could be the issue here? Thank you so much! Here is the error message: here is an excerpt of my chatstate.js: …
Rainer Winkler
  • 235
  • 2
  • 12
4
votes
2 answers

How do I only wrap some pages in a Context Provider with Next.js?

I have three pages that I want to share data between (these are the core of the web app) but I also have a bunch of blog pages that don't care about that data. Everywhere I've looked suggests putting the Provider in the _app.tsx file. If I…
mpc75
  • 617
  • 2
  • 14
4
votes
2 answers

useContext inside axios interceptor

I cant figure out why my useContext is not being called in this function: import { useContext } from "react"; import { MyContext } from "../contexts/MyContext.js"; import axios from "axios"; const baseURL = "..."; const axiosInstance =…
4
votes
4 answers

React: useContext value is not updated in the nested function

I have a simple context that sets some value that it get from backend, pseudo code: export const FooContext = createContext(); export function Foo(props) { const [value, setValue] = useState(null); useEffect(() => { …
Bob Sacamano
  • 447
  • 9
  • 26
3
votes
0 answers

React useContext not setting the value at first time

I am newbie to react and i am trying to learn useReducer and useContext. What i am trying to do is add the product to the cart and adding it to the context variable by using setContext, const [context, setContext] = useContext(Context) const [cart,…
3
votes
3 answers

How can i share data between non-related components in react js?

I'm quite new with reactjs, and I'm struggling with the following: I have a Header component (in my App.js file) which contains an AudioPlayer. This AudioPlayer needs to be updated with current value of src (with the path of the song, stored in…
3
votes
4 answers

How to fix my React Context to render useEffect and reload table data

I have an input form that selects data to be rendered inside my tables. The data only appears after a forced page reload. I would like for the data to re-render with the expected data entered by the user after each submit in my input. To do so I…
woof
  • 82
  • 2
  • 4
  • 16
3
votes
1 answer

In functional component useContext works in console.log but returns error on render

I have been looking around, but can't find a definite answer. I have wrapped my app in , I have passed props and tried return
{props.user.username}
but nothing works, just getting the same TypeError: Cannot read property…
Smlok
  • 325
  • 1
  • 10
3
votes
1 answer

ContextProvider doesn't mount when page refreshes

Please see the full code on codesandbox. I have two Route components for my countries api app- <>
Sapinder
  • 183
  • 10
1
2 3
20 21