Reactjs 如何从ES6中的构造函数获取状态值?
我试图继续使用ES6语法,但在尝试检索状态值时出错 所以我的问题是:如何在ES6中获取状态值?以下是代码的一部分:Reactjs 如何从ES6中的构造函数获取状态值?,reactjs,react-native,ecmascript-6,Reactjs,React Native,Ecmascript 6,我试图继续使用ES6语法,但在尝试检索状态值时出错 所以我的问题是:如何在ES6中获取状态值?以下是代码的一部分: constructor(props) { super(props); this.state = { timeElapsed: null, isRunning: false } } 然后,当我尝试获取isRunning状态时,它会给我以下错误:无法读取未定义的属性“isRunning” if (this.state.isRun
constructor(props) {
super(props);
this.state = {
timeElapsed: null,
isRunning: false
}
}
然后,当我尝试获取isRunning状态时,它会给我以下错误:无法读取未定义的属性“isRunning”
if (this.state.isRunning) {
clearInterval(this.interval);
this.setState({
isRunning: false
});
return
}
有什么想法吗?谢谢
编辑(以下是完整代码):
import React,{Component}来自'React';
进口{
文本,
看法
评估学,
样式表,
可触摸高光
}从“反应本机”;
从“力矩”中导入力矩;
从“分-秒-毫秒”导入formatTime;
类秒表扩展组件{
建造师(道具){
超级(道具);
此.state={
时间流逝:空,
isRunning:false
}
}
render(){
返回(
{formatTime(this.state.timeappeased)}
{this.startStopButton()}
{this.lapButton()}
圈数表
)
}
startStopButton(){
var style=this.state.isRunning?styles.startButton:styles.stopButton;
返回(
{this.state.isRunning?'Stop':'Start'}
)
}
lapputton(){
返回(
大腿部
)
}
边框(颜色){
返回{
边框颜色:颜色,
边框宽度:4
}
}
手稿{
log('按下启动按钮');
如果(此.state.isRunning){
clearInterval(这个.interval);
这是我的国家({
isRunning:false
});
返回
}
var startTime=新日期();
this.interval=setInterval(
()=>{
这是我的国家({
已用时间:新日期()-startTime
})
}, 30
);
这是我的国家({
isRunning:对
})
}
拉普雷斯(){
console.log('Lap已按下');
}
}
var styles=StyleSheet.create({
容器:{//主容器
弹性:1,
对齐项目:“拉伸”
},
标题:{//黄色
弹性:2
},
页脚:{//蓝色
弹性:3
},
计时器记录器:{
弹性:5,
为内容辩护:“中心”,
对齐项目:“中心”
},
计时器:{
尺寸:60
},
按钮振打器:{
弹性:3,
flexDirection:'行',
为内容辩护:“周围的空间”,
对齐项目:“中心”
},
按钮:{
边界宽度:2,
身高:100,
宽度:100,
边界半径:50,
为内容辩护:“中心”,
对齐项目:“中心”
},
开始按钮:{
边框颜色:“红色”
},
停止按钮:{
边框颜色:“绿色”
},
按钮:{
边框颜色:“蓝色”
}
});
//AppRegistry.registerComponent('stopWatch',function(){
//返回秒表;
// });
AppRegistry.registerComponent('秒表',()=>秒表);
编辑2:
以下是一个在构造函数中有绑定和无绑定的组合解决方案:
import React, {Component} from 'react';
import {
Text,
View,
AppRegistry,
StyleSheet,
TouchableHighlight
} from 'react-native';
import Moment from 'moment';
import formatTime from 'minutes-seconds-milliseconds';
class StopWatch extends Component {
constructor(props) {
super(props);
this.state = {
timeElapsed: null,
isRunning: false
}
this.startStopButton = this.startStopButton.bind(this)
this.lapButton = this.lapButton.bind(this)
}
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<View style={styles.timerWrapper}>
<Text style={styles.timer}>{formatTime(this.state.timeElapsed)}</Text>
</View>
<View style={styles.buttonWrapper}>
{this.startStopButton()}
{this.lapButton()}
</View>
</View>
<View style={styles.footer}>
<Text>List of laps</Text>
</View>
</View>
)
}
startStopButton() {
var style = this.state.isRunning ? styles.startButton : styles.stopButton;
handleStartPress = () => {
console.log('Start was pressed');
if (this.state.isRunning) {
clearInterval(this.interval);
this.setState({
isRunning: false
});
return
}
var startTime = new Date();
this.interval = setInterval(
()=>{
this.setState({
timeElapsed: new Date() - startTime
})
}, 30
);
this.setState({
isRunning: true
})
}
return (
<TouchableHighlight style={[styles.button, style]} onPress={handleStartPress} underlayColor="gray">
<Text>{this.state.isRunning ? 'Stop' : 'Start'}</Text>
</TouchableHighlight>
)
}
lapButton() {
handleLapPress = () => {
console.log('Lap was pressed');
}
return (
<TouchableHighlight style={[styles.button, styles.lapButton]} onPress={handleLapPress} underlayColor="gray">
<Text>Lap</Text>
</TouchableHighlight>
)
}
border(color) {
return {
borderColor: color,
borderWidth: 4
}
}
}
var styles = StyleSheet.create({
container: { // Main container
flex: 1,
alignItems: 'stretch'
},
header: { // Yellow
flex: 2
},
footer: { // Blue
flex: 3
},
timerWrapper: {
flex: 5,
justifyContent: 'center',
alignItems: 'center'
},
timer: {
fontSize: 60
},
buttonWrapper: {
flex: 3,
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center'
},
button: {
borderWidth: 2,
height: 100,
width: 100,
borderRadius: 50,
justifyContent: 'center',
alignItems: 'center'
},
startButton: {
borderColor: 'red'
},
stopButton: {
borderColor: 'green'
},
lapButton: {
borderColor: 'blue'
}
});
AppRegistry.registerComponent('stopwatch', () => StopWatch);
class StopWatch extends Component {
constructor(props) {
super(props);
this.state = {
timeElapsed: null,
isRunning: false
}
this.startStopButton= this.startStopButton.bind(this)
this.lapButton = this.lapButton.bind(this)
this.handleStartPress = this.handleStartPress.bind(this)
this.handleLap = this.handleLap.bind(this)
}
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<View style={styles.timerWrapper}>
<Text style={styles.timer}>{formatTime(this.state.timeElapsed)}</Text>
</View>
<View style={styles.buttonWrapper}>
{this.startStopButton()}
{this.lapButton()}
</View>
</View>
<View style={styles.footer}>
<Text>List of laps</Text>
</View>
</View>
)
}
startStopButton() {
var style = this.state.isRunning ? styles.startButton : styles.stopButton;
return (
<TouchableHighlight style={[styles.button, style]} onPress={this.handleStartPress} underlayColor="gray">
<Text>{this.state.isRunning ? 'Stop' : 'Start'}</Text>
</TouchableHighlight>
)
}
lapButton() {
return (
<TouchableHighlight style={[styles.button, styles.lapButton]} onPress={this.lapPress} underlayColor="gray">
<Text>Lap</Text>
</TouchableHighlight>
)
}
border(color) {
return {
borderColor: color,
borderWidth: 4
}
}
handleStartPress() {
console.log('Start was pressed');
if (this.state.isRunning) {
clearInterval(this.interval);
this.setState({
isRunning: false
});
return
}
var startTime = new Date();
this.interval = setInterval(
()=>{
this.setState({
timeElapsed: new Date() - startTime
})
}, 30
);
this.setState({
isRunning: true
})
}
lapPress() {
console.log('Lap was pressed');
}
}
import React,{Component}来自'React';
进口{
文本,
看法
评估学,
样式表,
可触摸高光
}从“反应本机”;
从“力矩”中导入力矩;
从“分-秒-毫秒”导入formatTime;
类秒表扩展组件{
建造师(道具){
超级(道具);
此.state={
时间流逝:空,
isRunning:false
}
this.startStopButton=this.startStopButton.bind(this)
this.lapButton=this.lapButton.bind(this)
}
render(){
返回(
{formatTime(this.state.timeappeased)}
{this.startStopButton()}
{this.lapButton()}
圈数表
)
}
startStopButton(){
var style=this.state.isRunning?styles.startButton:styles.stopButton;
扶手压杆=()=>{
log('按下启动按钮');
如果(此.state.isRunning){
clearInterval(这个.interval);
这是我的国家({
isRunning:false
});
返回
}
var startTime=新日期();
this.interval=setInterval(
()=>{
这是我的国家({
已用时间:新日期()-startTime
})
}, 30
);
这是我的国家({
isRunning:对
})
}
返回(
{this.state.isRunning?'Stop':'Start'}
)
}
lapputton(){
HandleAppress=()=>{
console.log('Lap已按下');
}
返回(
大腿部
)
}
边框(颜色){
返回{
边框颜色:颜色,
边框宽度:4
}
}
}
var styles=StyleSheet.create({
容器:{//主容器
弹性:1,
对齐项目:“拉伸”
},
标题:{//黄色
弹性:2
},
页脚:{//蓝色
弹性:3
},
计时器记录器:{
弹性:5,
为内容辩护:“中心”,
对齐项目:“中心”
},
计时器:{
尺寸:60
},
按钮振打器:{
弹性:3,
flexDirection:'行',
为内容辩护:“周围的空间”,
对齐项目:
class StopWatch extends Component {
constructor(props) {
super(props);
this.state = {
timeElapsed: null,
isRunning: false
}
this.startStopButton= this.startStopButton.bind(this)
this.lapButton = this.lapButton.bind(this)
this.handleStartPress = this.handleStartPress.bind(this)
this.handleLap = this.handleLap.bind(this)
}
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<View style={styles.timerWrapper}>
<Text style={styles.timer}>{formatTime(this.state.timeElapsed)}</Text>
</View>
<View style={styles.buttonWrapper}>
{this.startStopButton()}
{this.lapButton()}
</View>
</View>
<View style={styles.footer}>
<Text>List of laps</Text>
</View>
</View>
)
}
startStopButton() {
var style = this.state.isRunning ? styles.startButton : styles.stopButton;
return (
<TouchableHighlight style={[styles.button, style]} onPress={this.handleStartPress} underlayColor="gray">
<Text>{this.state.isRunning ? 'Stop' : 'Start'}</Text>
</TouchableHighlight>
)
}
lapButton() {
return (
<TouchableHighlight style={[styles.button, styles.lapButton]} onPress={this.lapPress} underlayColor="gray">
<Text>Lap</Text>
</TouchableHighlight>
)
}
border(color) {
return {
borderColor: color,
borderWidth: 4
}
}
handleStartPress() {
console.log('Start was pressed');
if (this.state.isRunning) {
clearInterval(this.interval);
this.setState({
isRunning: false
});
return
}
var startTime = new Date();
this.interval = setInterval(
()=>{
this.setState({
timeElapsed: new Date() - startTime
})
}, 30
);
this.setState({
isRunning: true
})
}
lapPress() {
console.log('Lap was pressed');
}
}