Home > Back-end >  How to update async storage in cleanup phase?
How to update async storage in cleanup phase?

Time:05-13

I have updated the local storage with some values in componentDidMount() and want to change/update the local storage on unmount phase. How can I achieve a local storage update with a specific value in cleanup/unmount?

CodePudding user response:

This might help

class App extends Component {

  componentWillUnmount() {
    this.storeData("xxx");
  }

  storeData = async (value) => {
    try {
      await AsyncStorage.setItem("key", value);
    } catch (e) {
      console.log(e);
    }
  };
}

CodePudding user response:

You can use componentWillUnmount() for class components

componentWillUnmount() {
    // updated local storage
}

or use a cleanup function in a useEffect hook for functional components

useEffect(() => {
    // do something on mount

    return () => {
      //update local storage on unmount
    }
}, [])
  • Related