【问题标题】:React Native Saving Input DataReact Native 保存输入数据
【发布时间】:2016-09-30 03:37:13
【问题描述】:

我想要两个文本字段:

  • 接受标题
  • 另一个接受正文(即更多文本)

...还有一个提交按钮:

  • 点击时保存输入的标题和正文

我研究了 TextInputAsyncStorage、TouchableHighlight 和 Navigator 组件以及一堆 react-native 教程。我似乎找不到任何一致性——甚至从 react-native 文档中也找不到。

这是我目前所拥有的:

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);

我可以在输入字段中输入,但无法确定 AsyncStorage,或者如何发布 new 消息而不是覆盖现有消息。我主要是在该领域寻求帮助 - 下面我发布了我的目标,以防出现我为什么要这样做的问题。

目标:

然后应该将保存的“帖子”打印到视图中,可以在其中按下(点击?)以显示正文的内容。

每次提交标题和正文时,都应将它们保存为新的“帖子”,而不是被覆盖。

【问题讨论】:

  • 异步存储应该可以工作。有什么问题。

标签: javascript reactjs react-native


【解决方案1】:

如果你想为此使用异步,你需要一个函数来保存数据:

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

并且您需要将此绑定到您的实例:

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

并检索您的消息以供以后使用:

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

【讨论】:

  • 我们有办法确保特定的数据类型吗?我收到错误Entries must be arrays of the form [key: string, value: string], got: ( messages ) - 我在设置messages 数组时将undefined 更改为null,因为否则它不会接受它。我现在在 JSON 错误页面中看到我的标题和正文...(似乎它需要一个特定的字符串)...这是 JSON 输出后的附加错误消息:NSMutableArray cannot be converted to NSString
  • 啊,是的,我已经更新了我的答案 - AS 只接受字符串,所以你需要序列化/反序列化你的数据。
  • 谢谢。这太好了,我不知道 stringify。此外,对于未来的读者,if (res === undefined) 需要更改为 if (res === null)
猜你喜欢
  • 2017-10-30
  • 2018-03-08
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多