2016-04-29 44 views

回答

0

基本上,你必須使用反應生命週期方法在組件...

var Box = React.createClass({ 
    getInitialState: function() { 
    return {windowWidth: window.innerWidth}; 
    }, 

    handleResize: function(e) { 
    this.setState({windowWidth: window.innerWidth}); 
    }, 

    componentDidMount: function() { 
    window.addEventListener('resize', this.handleResize); 
    }, 

    componentWillUnmount: function() { 
    window.removeEventListener('resize', this.handleResize); 
    }, 

    render: function() { 
    return <div>Current window width: {this.state.windowWidth}</div>; 
    } 
}); 

ReactDOM.render(<Box />, mountNode); 

我覺得這個鏈接應該幫助:)

https://facebook.github.io/react/tips/dom-event-listeners.html

相關問題