2014-07-09 2 views
3

상태가 변경된 후 이후에 초기 상태 인 을 검색 할 수 있습니까? F.ex :React : 상태가 변경된 후 초기 상태가됩니다.

React.createClass({ 
    getInitialState: function() { 
    return { foo: 'bar' } 
    }, 
    componentWillMount: function() { 
    this.setState({ foo: 'foo' }) 
    }, 
    componentDidMount: function() { 
    // get the initial state "bar" ? 
    } 
}) 

문서에서 아무것도 찾을 수 없습니다. 물론 외부 변수에 값을 저장할 수도 있지만 초기 상태를 다시 사용할 수있는 "config"객체로 처리 할 수 ​​있다면 난 골동품입니다.

답변

6

아니요, 초기 상태는 저장되지 않지만 기능을 다시 실행하려면 this.getInitialState()으로 전화 할 수 있습니다.

0

그냥 변수에 초기 상태를 저장합니다

React.createClass({ 
    initialState: { foo: 'bar' }, 

    getInitialState : function() { 
     return this.initialState; 
    }, 
    componentWillMount: function() { 
     this.setState({ foo: 'foo' }) 
    }, 
    componentDidMount : function() { 
     console.log(this.initialState.foo); // Logs 'bar' 
    } 
}); 
관련 문제