Questions tagged [react-context]

React Context provides a way to pass data through the component tree without having to pass props down manually at every level.

Official React Context Docs

1056 questions
222
votes
5 answers

React Context vs React Redux, when should I use each one?

React 16.3.0 was released and the Context API is not an experimental feature anymore. Dan Abramov (the creator of Redux) wrote a good comment here about this, but it was 2 years when Context was still an Experimental feature. My question is, in your…
Alfrex92
  • 4,199
  • 4
  • 21
  • 37
133
votes
3 answers

How to update React Context from inside a child component?

I have the language settings in the context as like below class LanguageProvider extends Component { static childContextTypes = { langConfig: PropTypes.object, }; getChildContext() { return { langConfig: 'en' }; } render() { …
mshameer
  • 1,831
  • 2
  • 10
  • 15
101
votes
5 answers

Access React Context outside of render function

I am developing a new app using the new React Context API instead of Redux, and before, with Redux, when I needed to get a list of users for example, I simply call in componentDidMount my action, but now with React Context, my actions live inside my…
Gustavo Mendonça
  • 1,545
  • 3
  • 13
  • 22
79
votes
2 answers

How to change the value of a Context with useContext?

Using the useContext hook with React 16.8+ works well. You can create a component, use the hook, and utilize the context values without any issues. What I'm not certain about is how to apply changes to the Context Provider values. 1) Is the…
Randy Burgess
  • 3,842
  • 5
  • 35
  • 57
68
votes
7 answers

_react.default.createContext is not a function when using react-redux

I have a problem when adding components to the entry point, this error immediately pops up here, how to fix it? I also try add only Main component but anyway i take that error, in main.jsx just class with render method return "hello…
Drop
  • 1,235
  • 1
  • 13
  • 20
67
votes
5 answers

How to update the Context value in Provider from the Consumer

MyContext.js import React from "react"; const MyContext = React.createContext('test'); export default MyContext; Created A context separate js file where i can access in my parent as well as my child component Parent.js import MyContext from…
Nowshad Syed
  • 713
  • 1
  • 6
  • 6
52
votes
2 answers

Does new React Context API trigger re-renders?

I have been trying to understand the new React Context API and was playing with it. I just wanted to check a simple case - what all re-renders when data to a Provider is updated. Check this small example on Codesandbox So, in my example, I have an…
Sachin
  • 2,392
  • 2
  • 13
  • 28
39
votes
6 answers

Cannot read property 'history' of undefined (useHistory hook of React Router 5)

I am using the new useHistory hook of React Router, which came out a few weeks ago. My React-router version is 5.1.2. My React is at version 16.10.1. You can find my code at the bottom. Yet when I import the new useHistory from react-router, I get…
35
votes
2 answers

React createContext issue in Typescript?

So I'm having a very weird issue with React Context + Typescript. Working example In the above example, you can see what I'm trying to do actually work. Essentially I'm managing state with the new useContext method, and it works perfectly. However,…
bauervision
  • 585
  • 1
  • 6
  • 21
35
votes
2 answers

Jest mock react context

I need some help understanding how one can test an application using React Context. Here's my sample setup. context.js import React from 'react' export const AppContext = React.createContext() App.js import React from 'react' import MyComponent…
artooras
  • 4,425
  • 4
  • 34
  • 59
32
votes
3 answers

Apollo Client Cache vs. Redux

I'm trying to migrate from Redux Store to use Apollo Client Cache that comes with Apollo Graphql Client. One of the key features that sets Apollo Client apart from other data management solutions is its normalized cache. Just by setting up Apollo…
30
votes
5 answers

Too many React Context providers

New to react here and trying to wrap my head round the new Context API (I haven't looked into Redux etc. yet). Seems I can do much of what I need to do, but I'm going to end up with lots and lots of providers, all needing a tag to wrap my main…
jonhobbs
  • 21,469
  • 23
  • 94
  • 144
28
votes
2 answers

React Context API - persist data on page refresh

Let's say we have a context provider set up, along with some initial data property values. Somewhere along the line, let's say a consumer then modifies those properties. On page reload, those changes are lost. What is the best way to persist the…
Cog
  • 533
  • 2
  • 6
  • 17
27
votes
5 answers

How to get the data from React Context Consumer outside the render

I am using the new React Context API and I need to get the Consumer data from the Context.Consumer variable and not using it inside the render method. Is there anyway that I can achieve this? For examplify what I…
Gustavo Mendonça
  • 1,545
  • 3
  • 13
  • 22
20
votes
4 answers

How should the new context api work with React Native navigator?

I created a multiscreen app using React Navigator following this example: import { createStackNavigator, } from 'react-navigation'; const App = createStackNavigator({ Home: { screen: HomeScreen }, Profile: { screen: ProfileScreen…
Tom
  • 7,011
  • 6
  • 34
  • 50
1
2 3
70 71