Skip to content

Commit

Permalink
修复请求返回数据展示为null的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Feb 4, 2018
1 parent fa4c3f0 commit 342bb98
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/pages/Github.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class Github extends Component {
constructor(props) {
super(props);
this.state = {
content: githublist || 'loading...',
content: githublist,
option: [
{
label: '今天',
Expand Down Expand Up @@ -45,9 +45,11 @@ export default class Github extends Component {
return url;
}
getTrending(type) {
// const { content } = this.state;
const localContent = localStorage.getItem('github-list');
if (!localContent) type = 'select';
const getDate = type === 'select' ? fetchTimely(this.getURL()) : fetchInterval(this.getURL(), 3, 'github-trending');
getDate.then((response) => {
if (!this.mounted) return;
response.replace(/<body\b[^>]*>([\s\S]*?)<\/body>/gi, (node, body) => {
response = body;
return node;
Expand All @@ -62,15 +64,16 @@ export default class Github extends Component {
// 清除头像,避免被和谐
$('.f6 .mr-3').not('.mr-3:first-child').empty();
const _html = $('div.explore-content').html();
if (!_html) return;
localStorage.setItem('github-list', _html);
if (!this.mounted) return;
this.setState({
content: _html,
}, () => {
localStorage.setItem('github-list', _html);
});
}).catch(() => {
if (!this.mounted) return;
this.setState({
content: githublist || '请求错误,请检查网路!',
content: githublist || '请求错误,请检查网路,或者重新刷新请求数据!',
});
});
}
Expand All @@ -92,7 +95,7 @@ export default class Github extends Component {
<Select onSelect={this.onSelect.bind(this, 'lang')} value={this.state.lang} option={this.state.optionLang} />
</div>
</div>
<div className={styles.list} dangerouslySetInnerHTML={{ __html: this.state.content }} />
<div className={styles.list} dangerouslySetInnerHTML={{ __html: this.state.content || 'loading...' }} />
{githublist && <Footer>已显示全部内容</Footer>}
</div>
);
Expand Down

0 comments on commit 342bb98

Please sign in to comment.