被拋出的錯誤是:意外的令牌,預計; (9:16) 這指向renderNumbers()函數的第一行。我的語法有什麼問題?我有點困惑,爲了讓它工作,需要改變什麼。反應 - 預期的意外令牌;
import React, { PropTypes } from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity
} from 'react-native';
renderNumbers() {
return this.props.numbers.map(n =>
<Text>{n}</Text>
);
}
export default class Counter extends React.Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.appName}>
Countly
</Text>
<Text style={styles.tally}>
Tally: {this.props.count}
</Text>
<Text style={styles.tally}>
Numbers: {this.props.numbers}
</Text>
<View>
{this.renderNumbers()}
</View>
<TouchableOpacity onPress={this.props.increment} style={styles.button}>
<Text style={styles.buttonText}>
+
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.props.decrement} style={styles.button}>
<Text style={styles.buttonText}>
-
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.props.power} style={styles.button}>
<Text style={styles.buttonText}>
pow
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.props.zero} style={styles.button}>
<Text style={styles.buttonText}>
0
</Text>
</TouchableOpacity>
</View>
);
}
}
Counter.propTypes = {
count: PropTypes.number,
numbers: PropTypes.func,
increment: PropTypes.func,
decrement: PropTypes.func,
zero: PropTypes.func,
power: PropTypes.func
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
appName: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
tally: {
textAlign: 'center',
color: '#333333',
marginBottom: 20,
fontSize: 25
},
button: {
backgroundColor: 'blue',
width: 100,
marginBottom: 20,
padding: 20
},
buttonText: {
color: 'white',
textAlign: 'center',
fontSize: 20
}
});
謝謝你的幫助。
對不起renderNumbers的兩倍使用
this
關鍵字。這不是問題。在發佈之前應該將其刪除。 – Wasteland@Wasteland只是檢查我的第一句話。 –
是不是隻用於無狀態組件的「函數」一詞?當它是一個類組件時,它應該是nameOfFunction(){}。我對嗎? – Wasteland