페이지 매김을 만들 때 Redux를 사용하고 있습니다. 내 문제는, constructor
URL을 구문 분석하고 페이지 매김에 대한 것이 있는지 확인하는 메소드를 실행했습니다. 그런 다음 데이터를 저장소에 저장하는 작업을 실행합니다. 모두 원활하게 돌아갑니다.React - constructor() 및 componentDidMount
그런 다음 데이터를 가져 오는 다른 작업을 실행할 때 componentDidMount
메서드가 있습니다. 그리고 나는 이전에 강요 한 소품들을 사용합니다. 불행히도, 가게는 초기 상태에 있습니다.
내 (간체) 코드 :
class NewsList extends React.Component {
constructor(props) {
super(props);
this.profile = document.getElementById('articles').getAttribute('data-profile');
this.parseHash();
}
parseHash() {
/* Method for parsing navigation hash
---------------------------------------- */
const hash = window.location.hash.replace('#', '').split('&');
const data = {
page: 1,
offset: 0
};
// hash parsing
this.props.setPagination(data);
}
componentDidMount() {
this.loadNews();
// If I use
// setTimeout(() => { this.loadNews(); })
// it works, but I feel this is hack-ish
}
loadNews() {
this.props.update({
current: {page: this.props.NewsListReducer.current.page, offset: this.props.NewsListReducer.current.offset},
next: {page: this.props.NewsListReducer.next.page, offset: this.props.NewsListReducer.next.offset}
});
}
}
I console.log(this.props.NewsListReducer)
, 내가 모두 current
및 next
개체에 대한 undefined
를 얻고있다,하지만 돌아 오는 DevTools로를 사용하는 경우, 데이터가있다. 내가 무엇을 할 수 있을지?
this.parseHash()를 componentWillMount()로 이동하십시오. – jzm
@jzm과 동일합니다. 'constructor'은'componentWillMount()'의 대체물과 같습니다. –
순수한 반응. 하지만 redux, 여기에 마지막 대답을 참조하십시오 : http://stackoverflow.com/a/37703446/769083 – jzm