Questions tagged [use-context]

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

586 questions
0
votes
1 answer

useContext hook returns undefined

Inside of App component I have Component1 which has a nested component. I create a contextApi with React.createContext() to use it inside of nested component . Using useContext() hook I am trying to get myname value from App.js and use it as style,…
Parviz Pirizade
  • 193
  • 2
  • 12
0
votes
0 answers

dispatch inside useEffect opposite result

I am displaying a foto in the front using Leigh Halliday's Image Previews in React with FileReader from https://www.youtube.com/watch?v=BPUgM1Ig4Po and everything is super BUT: 1.I want to get information from the image is displaying, exactly the…
0
votes
2 answers

Component is not getting updated value from Context

In Home component I am fetching product details from Product context. But when context value is changing the updated value is not coming in the Home component. Here is home component: import React, { useEffect, useState } from "react"; import {…
0
votes
1 answer

Passing function through useContext

I'm currently struggling with React Context. I'd like to pass functions allowing the show / hide cart logic in the context, instead of using props between components. I dont understand why when clicking on the button in HeaderCartButton component,…
0
votes
3 answers

How to satisfy typescript when using the context API?

I am currently writing my context API like so: import React, { useState, createContext, SetStateAction } from 'react' type LoginContextProps = [boolean, React.Dispatch>] export const LoginContext =…
lukeet
  • 461
  • 1
  • 4
  • 22
0
votes
1 answer

Error importing context with React native

I'm learning to use context with React native, actually i'm following a blog where define how use createContext and useContext for Authentication logic in any app with React and Navigation. In the blog not define how to import the class AuthService…
Dr oscar
  • 359
  • 1
  • 4
  • 16
0
votes
1 answer

Testing react component that uses Context - change state of component under test

I want to test the following React component import React, { useContext, useState } from "react"; import { IntlProvider } from "react-intl"; export const Context = React.createContext(); const defaultLocale = navigator.language; const…
zoran
  • 826
  • 1
  • 10
  • 14
0
votes
1 answer

Unable to get user Data passed as a context value by React useContext

I am using google login for my React application and I want to pass the response from google login i.e. user data to other react components using useContext hook but I'm not getting user data there. This is my Home.js file where I'm creating context…
0
votes
1 answer

useState values where useContext.Provider is defined

I am doing an app where I have some components and I've defined useStates from them, obtaining the values I need from all of them I've also created a Context with useContext and I wrapped my main component like this: import React from…
0
votes
1 answer

Can't use React context?

My index.js has: ReactDOM.render( , document.getElementById('root') ); Inside…
EZbrute
  • 400
  • 2
  • 14
0
votes
0 answers

Why my context state is not rendering in my unique component?

I am redirecting on my unique component and I want my data in my context api to be rendered matching with the params that I passed. Here is the code to my Component; export default function BasketballScoreboard() { const dataContext =…
0
votes
1 answer

How do I add/remove from an array with the reducer within React Context?

import React from 'react'; import {useReducer, createContext } from 'react'; let initialState = { associatedOrgs:[], }; const reducer = (state, action) => { switch (action.type) { case "logout": return initialState; case…
0
votes
1 answer

React useEffect useContext - context works with some components, but not in dynamic route

I am working on a React blog app and have posts in markdown files with metadata. The metadata is stored in Firestore. The .md files with the content are stored in Cloud Storage. The App component uses useEffect get the metadata for each post from…
Fruno
  • 81
  • 10
0
votes
2 answers

adding and removing data and counting with useContext - React - Typescript

I'm trying to understand useContext with what, I thought, would be a trivial example... I'm not putting the entire code in here but only relevant snippets, the full code is on codesandbox I have a page with two components. On the left is a list. On…
NiHaoLiHai
  • 23
  • 1
  • 8
0
votes
1 answer

Counting number of times added with useContext - React and Typescript

I am trying to understand the useContext hook a little bit better. I am playing around with this codesandbox which adds items from the left side component to the right side. Now, I would like to count the number of times they are added to the list…