Questions tagged [use-context]

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

586 questions
0
votes
1 answer

How to pass data using useContext hook?

I am trying to pass the petData via context to the SearchPage component. The petData is returning undefined. Any help in what I am doing wrong. Thanks Context.js import { createContext } from "react"; const PetDataContext = createContext(); export…
coolps811
  • 193
  • 4
  • 11
0
votes
0 answers

How to correctly check for a value in react useContext and return a statement

Im currently using React.useContext to store an array with objects like this: { enable: true, id: 1 } I made a switch that update the value inside the array from true to false. I've done the hard work and there is a bunch of code that don't need…
jan
  • 235
  • 1
  • 2
  • 12
0
votes
1 answer

async storage is not working when I reload the react-native app

I get to the point where after I log in I go to my Home and Profile screens, but if I reload or close the app and then open it again instead of open it on the Home screen, it goes to the login again. I tried with AsyncStorage to save the user until…
0
votes
1 answer

React form are rendering the previous value and not the current

My react form is using the useContext to save the value to a global state. Trying to change the value from 1-5 are rendering the previous number and not the current number. How can I change this behavior? Sandbox link…
jan
  • 235
  • 1
  • 2
  • 12
0
votes
1 answer

Testing a component within a context provider that provides state of parent component fails

I am trying to test my Login Component that is embedded in a Context. Provider in the App: function App() { const [user, setUser] = useState(null); const provideUser = useMemo(() => ({user, setUser}), [user, setUser]); useEffect(() =>…
SirNoob
  • 569
  • 1
  • 4
  • 18
0
votes
1 answer

React useContext losing state

I have a problem that I've been trying to solve for the past couple of days: I'm using a Context Provider for form fields and for whatever reason fields keep overwriting each other when I use memo. Provider: export const Context =…
Matt Brandt
  • 581
  • 1
  • 4
  • 20
0
votes
1 answer

How to use useContext() within a Compound Component?

I have a HeaderAvatarMenu component which is a dropdown menu, and it has a state of toggle. const ToggleContext = createContext(); const HeaderAvatarMenu = ({ children, ...restProps }) => { const [toggle, setToggle] = useState(false); return…
Sami Khan
  • 31
  • 1
  • 8
0
votes
0 answers

React Native useEffect loop on dependency

I have this basic useEffect const [dummy, setDummy] = useState('a'); const [bubble, setBubble] = useState([]); useEffect(() => { console.log('useEffect in Context'); setDummy('z'); console.log(dummy); }, [dummy]); the useEffect is…
popeating
  • 386
  • 1
  • 2
  • 16
0
votes
1 answer

Accessing Context in React Native

in react native i have the following files: App.js import { StyleSheet, Text, View } from 'react-native'; import Children from './Child'; import { MyProvider } from './context/Context'; import React from 'react'; const App = () => { return ( …
popeating
  • 386
  • 1
  • 2
  • 16
0
votes
1 answer

Hot to pass state from main App.js to screen components using react navigation in react native

I'm making a small app in react native, it's a basic login workflow that use a couple of screen (login and home), that are shown based on user state; in App.js i have a bunch of state that im using to enable/disbale views, buttons, show username and…
popeating
  • 386
  • 1
  • 2
  • 16
0
votes
0 answers

Passing Function With Parameters in Context in React

I have Function With Parameters in Context File and the Parameters need to pass it onClick, But it's show error "onLogin is not a function" Context.js export const AuthContext = createContext(undefined); export default Context({children}) { …
Ramy Mohamed
  • 236
  • 1
  • 5
  • 18
0
votes
1 answer

react context is giving undefined object

In my react application I am trying to use context api. In my component I am importing the context but it is giving error that object can not destructure the property. I am trying to implement cart functionality in my app. I am using…
Hasan Zubairi
  • 1,037
  • 4
  • 23
  • 57
0
votes
3 answers

ReferenceError: React is not defined for context

import './App.css'; import ComponentC from './components/ComponentC'; export const UserContext = React.createContext() function App() { return (
0
votes
1 answer

undefined is not an object (evaluating 'context._context') in react-native

I am trying to implement useContext in my react-native app but getting an error as: "undefined is not an object (evaluating 'context._context')". here is my code(Trying to use the EmailContext): import React, {useState} from 'react'; export const…
0
votes
2 answers

useContext is not displaying the updated state (when data is retrieved from firebase in useEffect hook in global file) in child component

I am using context api to have a global state of products, for this I have ProductsContext.js I have an empty state of array defined like this in ProductsContext.js const [products, setProducts] = useState([]); In the same file (ProductsContext.js)…