score:3

Accepted answer

If you want to use Async for this you'll need a function to save the data:

_onPressButton () {
  // Get the data
  let title = this.state.messageTitle
  let message = this.state.messageBody

  // Retrieve the existing messages
  AsyncStorage.getItem('messages', (res) => {
    var messages

    // If this is the first time, set up a new array
    if (res === null) {
      messages = []
    }else {
      messages = JSON.parse(res)
    }

    // Add the new message
    messages.push({
      title: title,
      message: message
    })

    // Save the messages
    AsyncStorage.setItem('messages', JSON.stringify(messages), (res) => {})
  }
}

And you'll want to bind this to your instance:

<TouchableHighlight onPress={this._onPressButton.bind(this)} style={styles.button}>

And to retrieve your messages for use later:

AsyncStorage.getItem('messages', (res) => {
  this.setState({
    messages: res
  })
})

Related Query

More Query from same tag