调用方法时状态消失

时间:2018-04-29 15:44:08

标签: react-native state

我正在研究一个班级项目,我的州正在消失。在componentDidMount之后,console.log(this.state)就可以了。我启动setInterval并调用inc()。不知怎的,当我输入inc()时,状态就会消失。

import React from 'react';
import { TextInput,Button,StyleSheet, Text, View } from 'react-native';
import styles from './styles/styles.js';

debug=true

export default class App extends React.Component {
  constructor(){
    super()
    this.state={timer:'WORK',
                workTime: 25*60+0,
                breakTime: 5*60+0,
                currentTime:0,
                remainingTime:null,
                min:0,
                sec:0,
                startFlag:false,
                resetFlag:false}
  }
  componentDidMount(){
    this.interval=setInterval(this.inc,10000)
    if(debug)console.log('COMPONENTDIDMOUNT',this.state)
  }
  static getDerivedStateFromProps(nextProps, prevState) {
    if(debug)console.log('GETDERIVEDSTATEFROMPROPS',prevState)
  return null
  }
  shouldComponentUpdate(nextProps,nextState){
    if(debug)console.log('SHOULDCOMPONENTUPDATE',nextState)
    return true
  }
  componentDidUpdate(){
   if(debug)console.log('COMPONENTDIDUPDATE',this.state)
  }
  componentWillUnmount(){
   if(debug)console.log('COMMPONENTWILLUNMOUNT',this.state)
  }

  startToggle(){
    if(endTime === null)this.setState({remainingTime:this.state.workTime,
                                         startFlag:!this.state.startToggle})
    else this.setState({remainingTime:!this.state.startFlag})
  }
  textTime(){
    let min = Math.floor(this.state.remainingTime / 60).toString()
    let sec = (this.state.remainingTime % 60)
    if (sec < 10)sec ? '0' + sec : sec.toString()
    this.setState({min:min,sec:sec})
  }

  inc(){
    console.log(this.state)
  }

  captureInput(){}

  render() {
    console.log('RENDER',this.state)


    return (
      <View style={styles.container}>
        <Text style={styles.bigFont}>{`${this.state.timer + 'TIMER'}`}</Text>
        <Text style={styles.bigFont}>12:00</Text>
        <View style={styles.button}>
          <Button title='START' onPress={()=>this.startToggle()} />
          <Button title='RESET' onPress={()=>this.resetToggle()} />
        </View>
             <View style={styles.row}>
                <Text style={[styles.bold,{marginRight:10},{width:112},
                            {textAlign:'right'}]}>
                            'Work Timer:'</Text>
                <Text style={styles.bold}> min:</Text>
                <TextInput
                   defaultValue='50'
                   style={styles.input}
                   onChangeText={(text) => {this.captureInput(text)}}
                />
                <Text style={styles.bold}> sec:</Text>
                <TextInput
                   defaultValue='50'
                   style={styles.input}
                   onChangeText={(text) => {this.captureInput(text)}}
                />
             </View>
             <View style={styles.row}>
                <Text style={[styles.bold,{marginRight:10},{width:112},
                            {textAlign:'right'}]}>
                            'Break Timer:'</Text>
                <Text style={styles.bold}> min:</Text>
                <TextInput
                   defaultValue='50'
                   style={styles.input}
                   onChangeText={(text) => {this.captureInput(text)}}
                />
                <Text style={styles.bold}> sec:</Text>
                <TextInput
                   defaultValue='50'
                   style={styles.input}
                   onChangeText={(text) => {this.captureInput(text)}}
                />
             </View>
      </View>
    )
  }
}

2 个答案:

答案 0 :(得分:1)

inc方法声明更改为

inc = () => {
  ...
}

根据您的代码,this中的inc()并未指代component,因此您也未获得state

希望这会有所帮助!

答案 1 :(得分:1)

您有两个选择:

  1. inc()更改为inc = () =>
  2. this.inc更改为this.inc.bind(this)