我已经安装了本机0.30,在更新和执行我在互联网上找到的一段旧代码时遇到了麻烦。
下面是旧的rn代码:
"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);下面是我更新它所做的工作:
/**
* 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时,屏幕上出现了以下错误:
SyntaxError /Users/***/Documents/pulps/index.ios.js: Unexpected token(53:44)你能告诉我哪里错了吗?
顺便问一下,这样的脚本使用普通的Javascript或ES6更好吗?谢谢!
发布于 2016-07-30 19:17:32
当以一种不允许的方式使用“返回”时,方法saveData中出现了一个错误。
你的代码:
saveData(value) {
return (
AsyncStorage.setItem("myKey", value);
this.setState({"myKey": value});
);
}在调用两个void方法时,不能在这里使用“返回”。就这样改变吧:
saveData(value) {
AsyncStorage.setItem("myKey", value);
this.setState({"myKey": value});
}https://stackoverflow.com/questions/38676154
复制相似问题