首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图将旧的react本机文件更新为0.30

试图将旧的react本机文件更新为0.30
EN

Stack Overflow用户
提问于 2016-07-30 16:36:22
回答 1查看 60关注 0票数 0

我已经安装了本机0.30,在更新和执行我在互联网上找到的一段旧代码时遇到了麻烦。

下面是旧的rn代码:

代码语言:javascript
复制
"use strict";

var React = require("react-native");

var {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TextInput,
    AsyncStorage,
} = React;

var ReactProject = React.createClass({

    componentDidMount: function() {
        AsyncStorage.getItem("myKey").then((value) => {
            this.setState({"myKey": value});
        }).done();
    },

    getInitialState: function() {
        return { };
    },

    render: function() {
        return (
            <View style={styles.container}>
                <Text style={styles.saved}>
                    {this.state.myKey}
                </Text>
                <View>
                    <TextInput
                        style={styles.formInput}
                        onChangeText={(text) => this.saveData(text)}
                        value="" />
                </View>
                <Text style={styles.instructions}>
                    Type something into the text box.  It will be saved to
                    device storage.  Next time you open the application, the saved data
                    will still exist.
                </Text>
            </View>
        );
    },

    saveData: function(value) {
        AsyncStorage.setItem("myKey", value);
        this.setState({"myKey": value});
    }

});

var styles = StyleSheet.create({
    container: {
        padding: 30,
        flex: 1,
        justifyContent: "center",
        alignItems: "stretch",
        backgroundColor: "#F5FCFF",
    },
    formInput: {
        flex: 1,
        height: 26,
        fontSize: 13,
        borderWidth: 1,
        borderColor: "#555555",
    },
    saved: {
        fontSize: 20,
        textAlign: "center",
        margin: 10,
    },
    instructions: {
        textAlign: "center",
        color: "#333333",
        marginBottom: 5,
        marginTop: 5,
    },
});

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

下面是我更新它所做的工作:

代码语言:javascript
复制
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
"use strict";

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

class pulps extends Component {

  componentDidMount() {
      AsyncStorage.getItem("myKey").then((value) => {
          this.setState({"myKey": value});
      }).done();
  }

  getInitialState() {
    return { };
  }

  render() {
    return (
            <View style={styles.container}>
                <Text style={styles.saved}>
                    {this.state.myKey}
                </Text>
                <View>
                    <TextInput
                        style={styles.formInput}
                        onChangeText={(text) => this.saveData(text)}
                        value="" />
                </View>
                <Text style={styles.instructions}>
                    Type something into the text box.  It will be saved to
                    device storage.  Next time you open the application, the saved data
                    will still exist.
                </Text>
            </View>
    );
  }

  saveData(value) {
        return (
        AsyncStorage.setItem("myKey", value);
        this.setState({"myKey": value});
        );
    }
}

var styles = StyleSheet.create({
    container: {
        padding: 30,
        flex: 1,
        justifyContent: "center",
        alignItems: "stretch",
        backgroundColor: "#F5FCFF",
    },
    formInput: {
        flex: 1,
        height: 26,
        fontSize: 13,
        borderWidth: 1,
        borderColor: "#555555",
    },
    saved: {
        fontSize: 20,
        textAlign: "center",
        margin: 10,
    },
    instructions: {
        textAlign: "center",
        color: "#333333",
        marginBottom: 5,
        marginTop: 5,
    },
});

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

但是,当我按下ios-simulator时,屏幕上出现了以下错误:

代码语言:javascript
复制
SyntaxError /Users/***/Documents/pulps/index.ios.js: Unexpected token(53:44)

你能告诉我哪里错了吗?

顺便问一下,这样的脚本使用普通的Javascript或ES6更好吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-30 19:17:32

当以一种不允许的方式使用“返回”时,方法saveData中出现了一个错误。

你的代码:

代码语言:javascript
复制
  saveData(value) {
        return (
        AsyncStorage.setItem("myKey", value);
        this.setState({"myKey": value});
        );
    }

在调用两个void方法时,不能在这里使用“返回”。就这样改变吧:

代码语言:javascript
复制
  saveData(value) {
      AsyncStorage.setItem("myKey", value);
      this.setState({"myKey": value});
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38676154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档