2016-03-24 33 views
1

öğretici kaynak: A Duruma Bileşen - https://facebook.github.io/react/Eğiticiden tsx'i nasıl uygularım?

Bu benim kodudur: html olarak

interface Props { 

} 

interface State { 
    secondsElapsed: number, 
} 


class Timer extends React.Component<Props, State> { 
    private interval: number; 

    getInitialState() { 
     return { secondsElapsed: 0 }; 
    } 
    tick() { 
     this.setState({ secondsElapsed: this.state.secondsElapsed + 1 }); 
    } 
    componentDidMount() { 
     this.interval = setInterval(this.tick, 1000); 
    } 
    componentWillUnmount() { 
     clearInterval(this.interval); 
    } 
    render() { 
     return (
      <div>Seconds Elapsed: {this.state.secondsElapsed}</div> 
     ); 
    } 
} 


ReactDOM.render(
    <Timer />, 
    document.getElementById('example') 
); 

, bu timer.js:26 Uncaught TypeError: Cannot read property 'secondsElapsed' of null döndürür. NULL neden saniyeElapsız?

Ne oldu?

cevap

3

React ES6 sınıfları, getInitialState yöntemine sahip değildir.

constructor() { 
    super(); 
    this.state = { 
     secondsElapsed: 0 
    }; 
} 

React docs about ES6-classes

: Bu yöntem size kurucu içinde elle durumuna ayarlayabilirsiniz sınıfları Tepki React.createClass({})

tarafından yaratıldı sadece bileşenlere sahip

İlgili konular