注意:我是React Native的新手。下面的代码应该是使用React Native的计算器运行。我遇到了这个计算器代码的按钮出现问题。代码运行时没有错误,所以我不明白为什么按钮没有出现。按钮不出现
下面是代码:
import React, { Component } from 'react';
import {
View,
Text,
AppRegistry,
StyleSheet,
} from 'react-native';
const inputButtons = [
[1, 2, 3, '/'],
[4, 5, 6, '*'],
[7, 8, 9, '-'],
[0, '.', '=', '+']
];
const Style = StyleSheet.create({
rootContainer: {
flex: 1
},
displayContainer: {
flex: 2,
backgroundColor: '#193441'
},
inputContainer: {
flex: 8,
backgroundColor: '#3E606F'
},
inputButton: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 0.5,
borderColor: '#91AA9D'
},
inputButtonText: {
fontSize: 22,
fontWeight: 'bold',
color: 'white'
},
inputRow: {
flex: 1,
flexDirection: 'row'
}
});
<View style={Style.rootContainer}>
<View style={Style.displayContainer}></View>
<View style={Style.inputContainer}></View>
</View>
export default class ReactCalculator extends Component {
render() {
return (
<View style={Style.rootContainer}>
<View style={Style.displayContainer}></View>
<View style={Style.inputContainer}>
{this._renderInputButtons()}
</View>
</View>
)
}
_renderInputButtons() {
let views = [];
for (var r = 0; r < inputButtons.length; r ++) {
let row = inputButtons[r];
let inputRow = [];
for (var i = 0; i < row.length; i ++) {
let input = row[i];
inputRow.push(
<InputButton value={input} key={r + "-" + i} />
);
}
views.push(<View style={Style.inputRow} key={"row-" + r}>{inputRow}</View>)
}
return views;
}
render() {
return (
<View style={{flex: 1}}>
<View style={{flex: 2, backgroundColor: '#193441'}}></View>
<View style={{flex: 8, backgroundColor: '#3E606F'}}></View>
</View>
)
}
}
最新代码: - 接收错误,当我没有收到错误,按钮出现在不正确的地方。
import React, { Component } from 'react';
import {
View,
Text,
AppRegistry,
StyleSheet,
Button,
TouchableHighlight,
} from 'react-native';
const inputButton = [
[1, 2, 3, '/'],
[4, 5, 6, '*'],
[7, 8, 9, '-'],
[0, '.', '=', '+']
];
const Style = StyleSheet.create({
rootContainer: {
flex: 1
},
displayContainer: {
flex: 2,
backgroundColor: '#193441'
},
inputContainer: {
flex: 8,
backgroundColor: '#3E606F'
},
inputButton: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 0.5,
borderColor: '#91AA9D'
},
inputButtonText: {
fontSize: 22,
fontWeight: 'bold',
color: 'white'
},
inputRow: {
flex: 1,
flexDirection: 'row'
}
});
<View style={Style.rootContainer}>
<View style={Style.displayContainer}></View>
<View style={Style.inputContainer}></View>
</View>
export default class ReactCalculator extends Component {
render() {
return (
<TouchableHighlight style={Style.inputButton}
underlayColor="#193441"
onPress={this.props.onPress}>
<Text style={Style.inputButtonText}>{this.props.value}</Text>
</TouchableHighlight>
)
}
_renderInputButton() {
let views = [];
for (var r = 0; r < inputButton.length; r ++) {
let row = inputButton[r];
let inputRow = [];
for (var i = 0; i < row.length; i ++) {
let input = row[i].toString();
inputRow.push(
<InputButton
value={input}
onPress={this._onInputButtonPressed.bind(this, input)}
key={r + "-" + i}/>
);
}
_onInputButtonPressed(input) {
alert(input)
}
views.push(<View style={Style.inputRow} key={"row-" + r}>{inputRow}</View>)
}
return views;
}
}
尝试删除组件中的第二个渲染方法。 – tt9
@ user2313300我删除了_renderInputButtons()并没有发生任何事情。 – user1049876
不......你的代码中有两个render()函数。如果您的应用程序中的代码与您发布的代码相同,则在组件底部有第二个渲染功能 – tt9