useContext and useReducer Hooks not working. Error: Cannot read property 'state' of undefined

12,166

You can only access a context's value in a child component of the context Provider. In this case, you are calling useContext above where the Provider is rendered, within Store. In these cases, the default value passed to createContext is given. In this case, createContext(), no default value was given, so it is undefined. Hence trying to destructure undefined const { state, dispatch } = useContext(myContext); results in the error you are seeing.

Just adding an additional child component should make it work. Something like:

import React, { useState, useContext, useEffect, createContext } from            "react";
import { View, Text, StyleSheet, Button } from "react-native";
import Store, { myContext } from "./components/Store";

export default function AppWrapper(): JSX.Element {
  // Store, renders the provider, so the context will be accessible from App.
  return (
    <Store>
      <App />
    </Store>
  )
}

function App(): JSX.Element {
  const { state, dispatch } = useContext(myContext);

  return (
    <View style={styles.wrapper}>
      <Text>HEY</Text>
      <Text>Counter: {state}</Text>
      <Button title="Incr" onPress={() => dispatch(1)} />
      <Button title="Decr" onPress={() => dispatch(-1)} />
    </View>
  );
}

const styles = StyleSheet.create({
  wrapper: {
    marginTop: 100
  }
});
Share:
12,166

Related videos on Youtube

AmanDeepSharma
Author by

AmanDeepSharma

I'm passionate about contributing for an intelligent future and improving human life experience using software engineering and technical skills.

Updated on June 04, 2022

Comments

  • AmanDeepSharma
    AmanDeepSharma almost 2 years

    I'm trying to implement Redux concept in React Native using createContext, useReducer and useContext. Below are my code files:

    Store.tsx

    import React, { useReducer, createContext } from "react";
    import { View, Text, StyleSheet, Button } from "react-native";
    
    export const myContext = createContext();
    
    export default function Store(props) {
      const counter = 0;
      const [state, dispatch] = useReducer((state, action) => {
        return state + action;
      }, counter);
      return (
        <myContext.Provider value={{ state, dispatch }}>
          {props.children}
        </myContext.Provider>
      );
    }
    

    App.tsx

    import React, { useState, useContext, useEffect, createContext } from            "react";
    import { View, Text, StyleSheet, Button } from "react-native";
    import Store, { myContext } from "./components/Store";
    
    export default function App(): JSX.Element {
      const { state, dispatch } = useContext(myContext);
    
      return (
        <View style={styles.wrapper}>
          <Text>HEY</Text>
          <Store>
            <Text>Counter: {state}</Text>
            <Button title="Incr" onPress={() => dispatch(1)} />
            <Button title="Decr" onPress={() => dispatch(-1)} />
          </Store>
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      wrapper: {
        marginTop: 100
      }
    });
    

    I'm not sure why I'm not able to access 'State' in useContex. I get error "Cannot read property 'state' of undefined" Any help please. It would be very helpful if you can give some good explanation too with details.