Home > Mobile >  Clear the useContext state - React native
Clear the useContext state - React native

Time:05-26

I've been struggling with this problem for a few days, so any help would be greatly appreciated. We have a global data context which we're including data so I try to reset the useContext to initial state : null but it doesn't work. Any suggestion please!

All of my code can be found here:

Context.js:

import { createContext } from "react"

export const GlobalContext = createContext({})

App.js:

import { GlobalContext } from './src/helpers/Context'

  const [itemType, setItemType] = useState('')

 <GlobalContext.Provider value={{ itemType, setItemType }}>
      <PaperProvider>....

Screen1.js:

const { itemType, setItemType } = useContext(GlobalContext)

const onReset = () => {

    return Alert.alert(
      "..,
      "..",
      [

        {
          text: "Oui",
          onPress: () => {
                 setItemType(null)
            navigation.navigate('Home')
           
          },
        },

        // Does nothing but dismiss the dialog when tapped
        {
          text: "Non",
        },
      ]
    );

  
  }

CodePudding user response:

Do you have any reducers? You could reset some data in the context through them after the dispatch. See React - Clear form after submit with Context API state management

  • Related