我已经安装了本机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);
但是,当我按cmd + R时,我的
ios-simulator
屏幕上出现以下错误:SyntaxError /Users/***/Documents/pulps/index.ios.js: Unexpected token(53:44)
你能告诉我我哪里错了吗?
顺便问一句,使用纯Java脚本或ES6更好地适合这种脚本吗?
谢谢 !
最佳答案
使用不允许的方式使用“返回”时,在saveData方法中只是一个错误。
您的代码:
saveData(value) {
return (
AsyncStorage.setItem("myKey", value);
this.setState({"myKey": value});
);
}
调用两个void方法时,不能在此处使用“返回”。只需像这样更改它:
saveData(value) {
AsyncStorage.setItem("myKey", value);
this.setState({"myKey": value});
}
关于javascript - 尝试将旧的React Native文件更新为0.30,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38676154/