简体   繁体   English

反应本机保存输入数据

[英]React Native Saving Input Data

I would like to have two text fields: 我想有两个文本字段:

  • one that accepts a title 一个接受标题的人
  • another that accepts a body (ie more text) 接受身体的另一个人(即更多文字)

...and a submit button: ...和提交按钮:

  • that saves the title and body that was entered, when clicked 单击时保存输入的标题和正文

I have researched TextInput , AsyncStorage , TouchableHighlight and Navigator components as well as a bunch of react-native tutorials. 我研究过TextInputAsyncStorageTouchableHighlight和Navigator组件以及一堆反应原生教程。 I can't seem to find any consistency - not even from the react-native docs. 我似乎无法找到任何一致性 - 甚至不是来自本地反应文档。

Here is what I have so far: 这是我到目前为止:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  AsyncStorage,
  TextInput,
  TouchableHighlight
} from 'react-native';

class PostAndSave extends Component {

  constructor(props) {
    super(props);
    this.state = {
      messageTitle: '',
      messageBody: ''
    }
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Walker app
        </Text>
        <TextInput
          placeholder="Title"
          style={{height: 40, borderColor: 'gray', borderWidth: 1}}
          onChange={(event) => this.setState({messageTitle: event.nativeEvent.text})}
          value={this.state.messageTitle} />
        <TextInput
          placeholder="Body"
          style={{height: 40, borderColor: 'gray', borderWidth: 1}}
          onChange={(event) => this.setState({messageBody: event.nativeEvent.text})}
          value={this.state.messageBody} />
        <TouchableHighlight onPress={this._onPressButton} style={styles.button}>
          <Text style={styles.buttonText}>See all posts</Text>
        </TouchableHighlight>
      </View>
    );
  }
}

// styles here

AppRegistry.registerComponent('PostAndSave', () => PostAndSave);

I can type into the input fields but cannot figure AsyncStorage out, or how to post new messages as opposed to the overwriting the existing one. 我可以输入到输入字段,但找不出AsyncStorage出来,或如何,而不是在覆盖现有张贴新的讯息。 I'm mainly looking for help in that area - below I have posted my goal incase the question of why I want to do this comes up. 我主要是在那个领域寻求帮助 - 下面我已经发布了我的目标 ,这就是我想要做到这一点的问题。

Goal: 目标:

The saved 'post' should then be printed to a view, where it can be pressed (tapped?) to display the contents of the body. 然后应将保存的“帖子”打印到视图中,在该视图中可以按下(点击?)以显示正文的内容。

Each time a title and body are submitted they should be saved as a new 'post' and not overwritten. 每次提交标题和正文时,都应将其保存为新的“帖子”而不是覆盖。

If you want to use Async for this you'll need a function to save the data: 如果你想使用Async,你需要一个保存数据的功能:

_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
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM