【发布时间】:2016-03-31 08:11:16
【问题描述】:
我正在使用 ReactJS + Router 渲染我的网站服务器端 我的许多组件都会调用 REST 来生成内容。此内容不会作为 HTML 服务器端发送,因为它是异步调用。
一个可能看起来像这样的组件:
import React from 'react'
import ReactDOM from 'react-dom'
// Imports omitted
export default class MyPage extends React.Component {
constructor() {
super();
this.state = {items: []};
fetch("http://mywebservice.com/items").then((response) => {
response.json().then((json) => {
this.setState({items: json.items})
}).catch((error) => {});
});
}
render() {
if (this.state.items && this.state.items.length > 0) {
var rows = [];
// Go through the items and add the element
this.state.items.forEach((item, i) => {
rows.push(<div key={item.id}></div>);
});
return <div>
<table>
{rows}
</table>
</div>;
}
else {
return <span>Loading...</span>
}
}
}
搜索引擎会索引“正在加载...”,而我显然希望我的元素(项目)被索引。有没有办法解决这个问题?
【问题讨论】:
标签: reactjs react-router