我使用Redux來創建我的分頁。我的問題是,在constructor
我運行了一個方法,將解析URL並檢查是否有任何關於分頁。然後它運行一個將數據放入我的商店的操作。這一切運行順利。React - 構造函數()和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}
});
}
}
當我console.log(this.props.NewsListReducer)
,我越來越undefined
兩個current
和next
對象,但是當我使用終極版DevTools,數據是存在的。我能做什麼?
嘗試將this.parseHash()移入componentWillMount() – jzm
這與@jzm相同。 'constructor'就像'componentWillMount()'的替代品。 –
純粹的反應。但是對於redux,請參閱最後一個答案:http://stackoverflow.com/a/37703446/769083 – jzm