react native save data AsyncStorage

react native asyncstorage
react native asyncstorage best practices
@react-native-community/async-storage npm
expo asyncstorage
how to store image in asyncstorage in react native
react native asyncstorage login example
where does asyncstorage save data
react native asyncstorage callback example

Good day I'm using react native. I want to save the switch button to true or false, but it doesn't. When I open and close the application does not load the recorded data. Where is the problem. It does not save true or false.

class LoginForm extends Component {

  toggleRememberMe = value => {
  this.setState({ rememberMe: value })
    if (value === true) {
    this.rememberUser();
  } else {
    this.forgetUser();
  }
}

rememberUser = async () => {
try {
  await AsyncStorage.setItem('123123', this.state.remem);
} catch (error) {
  // Error saving data
}
};
getRememberedUser = async () => {
try {
  const username = await AsyncStorage.getItem('123123');
  if (username !== null) {
    return username;
  }
} catch (error) {
}
};
forgetUser = async () => {
  try {
    await AsyncStorage.removeItem('123123');
  } catch (error) {
  }
};

constructor(props) {
        super(props)
        const remem = this.getRememberedUser();
     this.state = {
      remem: remem || "", 
      rememberMe: remem ? true : false 
        }
    }
   render() {
        return (<Switch
  value={this.state.rememberMe}
  onValueChange={(value) => this.toggleRememberMe(value)}
  /><Text>Remember Me</Text>);
}

You will have to getRememberedUser() in componentDidMount.

Right now you have an async function that you're trying to execute outside of an async context.

Something like:

async componentDidMount() {
  const remem = await this.getRememberedUser();
  if (remem) {
    this.setState({rememberMe: remem})
  }
}

AsyncStorage · React Native, import { AsyncStorage } from 'react-native';. Persisting data: _storeData = async () => { try { await AsyncStorage.setItem( '@MySuperStore:key', 'I like to save it. AsyncStorage is React Native’s API for storing data persistently over the device. It’s a storage system that developers can use and save data in the form of key-value pairs. If you are coming from

your rememberUser() method is probably being called before state updates. setState() is an async operation. Try updating your asyncStore after rememberMe is set to value you are setting it to:

    toggleRememberMe = value => {
     this.setState({ rememberMe: value }, ()=>{
     if (value === true) {
     this.rememberUser();
    } else {
    this.forgetUser();
  }
})
}

How To Use React Native AsyncStorage, Saving the data. To save the data for the app to read, let us define an asynchronous helper method called saveData . This method is going to be  AsyncStorage in React Native to Store Data in Session. Contents. 1 React Native AsyncStorage. 2 To Import AsyncStorage in Code. 3 Store the value in AsyncStorage. 4 Get the value from the AsyncStorage. 5 To Make a React Native App. 6 Installation of Dependency. 7 CocoaPods Installation.

I have created a sandbox for you to get a look. Basically, moved the AsyncStorage to componentDidMount and removed some things (such as using the state for setting the AsyncStorage, this is needed because setState is async and your value couldn't be the expected)

React Native - AsyncStorage, React Native - AsyncStorage - In this chapter, we will show you how to persist your data using AsyncStorage. when the component is mounted. setName will take the text from our input field, save it using AsyncStorage and update the state. React Native has a built-in persistent key-value storage called AsyncStorage. It allows storing simple sets of data as text or serialized JSON objects. It picks the right approach of saving data depending on the platform used by an application. As you can guess from the name, it works asynchronously.

Saving data in local databases with AsyncStorage & React Native , In this tutorial, we'll learn how to use AsyncStorage in React Native to save data in a database. Browse other questions tagged asynchronous react-native local-storage or ask your own question. The Overflow Blog The Loop, June 2020: Defining the Stack Community

AsyncStorage in React Native to Store Data in Session, AsyncStorage can only be used to store small values in the form of a key and value pair because it has some limitations to store the data. You can store your value  Per the React Native documentation, the recommended way to persist data is through the AsyncStorage class. If you’re familiar with HTML5 local storage, then you’ll recognize that the two are very similar in how they function. AsyncStorage uses key-value pairs so to save data you might do something like this:

React Native Tutorial, React Native AsyncStorage is a simple, unencrypted, asynchronous, persistent, storage system which stores the data globally in the app. It store data in the form​  Browse other questions tagged json reactjs asynchronous react-native asyncstorage or ask your own question. The Overflow Blog Podcast 241: New tools for new times

Comments
  • It didn't work again. Thank you for the help
  • It does work. Is that all the code you have? Maybe there is something else