Questions tagged [use-context]

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

586 questions
0
votes
1 answer

Context not updating after API call to database

I am using useReducer inside of context which makes API calls to mongoDB to make changes to the database. When creating a new document in the collection organisation I then send back an object with the new organisation that was just created. What…
0
votes
1 answer

Is there a way i can get access of a component's state to another component through useReducer and Context API?

I found this code in a youtube video in which a counter in App.js is global and is being controlled by button in the app component as well as by buttons in other components by passing a function on click event of button that dispatches an…
Rija Shaheed
  • 69
  • 2
  • 7
0
votes
1 answer

React: Default Context Value is blank

I'm attempting to set a default value through a context (which I'll later update). However I can't seem to obtain the value in the consumer. I have a SummaryContext file which contains two default values title and summary. This is used in Body.js…
TheMightyLlama
  • 1,243
  • 1
  • 19
  • 51
0
votes
1 answer

React - useContext is returning undefined

I trying to use React context to manage state in my project, but I cannot seem to figure out why it's returning undefined. I copied the example from another project I was working on, and it seems as if everything should be working properly. I just…
monkeymynd
  • 69
  • 1
  • 1
  • 5
0
votes
1 answer

How do I store and get the theme state using AsyncStorage

I'm tying to store the theme state in the app, so that the chosen theme will be persistent after the app is closed. I have two files, one is the App.js file which has the navigation, and the other is the DrawerContent.js file that has the content of…
B_M
  • 175
  • 1
  • 11
0
votes
1 answer

POST Error 404(localhost:3000) || GET Error ERR_CONNECTION_REFUSED(localhost:5000) with socket.io/nodemoon/react

I am currently following a tutorial from webDevSimplified youtube : https://www.youtube.com/watch?v=tBr-PybP_9c github repo : https://github.com/WebDevSimplified/Whatsapp-Clone I have compared my code with the repo countless times and also read…
Aaron
  • 176
  • 3
  • 14
0
votes
0 answers

useReducer in useContext not saving state

I am trying to fetch data from my database and then using useReducer if the fetch is successful I then want to return it as state so I can pass it down to components. I have console.log'd req.data and it is fetching the correct data however it isn't…
0
votes
1 answer

Cannot read property 'map' of undefined - React Custom Hooks / useContext one works / one doesn't

I am currently following a tutorial on youtube building a whatsapp clone with react and socket.io from webDevSimplified: https://www.youtube.com/watch?v=tBr-PybP_9c and here the main repo :…
Aaron
  • 176
  • 3
  • 14
0
votes
0 answers

How to handle useContext when waiting for data to fetch

I am using useContext to fetch documents from MongoDB. I know that there is some amount of time that it takes for my app to fetch the data, save it to state and then pass the context to children. My question is - how do I handle the time inbetween…
0
votes
1 answer

implement useContext in a set of child components

I'm attempting to implement useContext in various areas of my application starting with the below. This excerpt references four separate part of the UI that nonetheless require information relating to an event. At the moment I'm prop drilling and…
TheMightyLlama
  • 1,243
  • 1
  • 19
  • 51
0
votes
1 answer

How to test code that uses a custom hook based on useContext with react-testing-library and jest

I've created a custom context hook - and I'm struggling to figure out how to pass values to its provider during testing. My hook: import React, { createContext, useContext, useState } from 'react'; const Context = createContext({}); export const…
Yonder
  • 719
  • 2
  • 13
  • 26
0
votes
2 answers

Can't perform a React state update on an unmounted component.(UseEffect)(Context API)(REACT.js)

So I am getting this Warning:- *Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup…
0
votes
1 answer

React context between unrelated components

For learning purposes I'm just trying to render this dumb example where Component A has a variable that creates a random number and another (unrelated) Component B can render it with useContext. I don't know how to make the provider of the context…
Za Fra
  • 13
  • 9
0
votes
1 answer

useContext to pass data from A

I am trying to pass data from a JSON file to a component with useContext: import sessionData from '../session.json'; import React from 'react'; export const SessionContext = React.createContext(sessionData); export const SessionProvider =…
Noble Polygon
  • 796
  • 11
  • 36
0
votes
1 answer

useState hook in context resets unfocuses input box

My project takes in a display name that I want to save in a context for use by future components and when posting to the database. So, I have an onChange function that sets the name in the context, but when it does set the name, it gets rid of focus…