Facebook React.js :: 为客户端渲染传递数据
Facebook React.js :: Pass data for Client Side rendering
我正在尝试使用 React.js 创建我的第一个 Web 应用程序。
我希望让 React 执行客户端呈现,但是呈现的页面由服务器托管数据库中保存的数据控制。
我应该如何将这些数据传递给 React?
我是否必须使用我的数据的 json 表示形式预先填充服务器端的每个 html 页面?
或者 React 可以直接从客户端访问我的数据库(或使用 RESTful API 调用)?
更新:001
我能找到的所有示例都在 node.js 上安装了 REACT.js。
我可以在 Apache Tomcat™、IBM Websphere Liberty Profile、Jetty 上使用 REACT.js 吗?
您提到的两种方法都可以工作,this blog post 给出了一个 服务器端 渲染和在渲染的 HTML 中嵌入道具的示例。 =14=]
编辑:
针对您的编辑,我想说 React 是一个 Javascript 框架,它自然应该部署在 javascript 服务器上,例如 Node. If you'd rather not use Node, you can for sure write client-only scripts that uses AJAX to request data from the API server. Here is a great example from Facebook。
它的一个片段:
componentDidMount: function() {
$.get(this.props.source, function(result) {
var lastGist = result[0];
if (this.isMounted()) {
this.setState({
username: lastGist.owner.login,
lastGistUrl: lastGist.html_url
});
}
}.bind(this));
}
安装组件后,以上代码使用来自服务器的数据填充 state
对象。
我正在尝试使用 React.js 创建我的第一个 Web 应用程序。
我希望让 React 执行客户端呈现,但是呈现的页面由服务器托管数据库中保存的数据控制。
我应该如何将这些数据传递给 React?
我是否必须使用我的数据的 json 表示形式预先填充服务器端的每个 html 页面?
或者 React 可以直接从客户端访问我的数据库(或使用 RESTful API 调用)?
更新:001
我能找到的所有示例都在 node.js 上安装了 REACT.js。 我可以在 Apache Tomcat™、IBM Websphere Liberty Profile、Jetty 上使用 REACT.js 吗?
您提到的两种方法都可以工作,this blog post 给出了一个 服务器端 渲染和在渲染的 HTML 中嵌入道具的示例。 =14=]
编辑:
针对您的编辑,我想说 React 是一个 Javascript 框架,它自然应该部署在 javascript 服务器上,例如 Node. If you'd rather not use Node, you can for sure write client-only scripts that uses AJAX to request data from the API server. Here is a great example from Facebook。
它的一个片段:
componentDidMount: function() {
$.get(this.props.source, function(result) {
var lastGist = result[0];
if (this.isMounted()) {
this.setState({
username: lastGist.owner.login,
lastGistUrl: lastGist.html_url
});
}
}.bind(this));
}
安装组件后,以上代码使用来自服务器的数据填充 state
对象。