从PHP渲染React组件 [英] Render React Components from PHP

查看:115
本文介绍了从PHP渲染React组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ReactJS为一个简单的可过滤项目列表提供支持,它可以很好地满足我的需求。

I'm using ReactJS to power a simple filterable item list and it works quite well for my need.

问题是我需要渲染标记服务器出于搜索引擎优化的原因,但当我调用 React.renderComponent()时,它将现有的标记替换为React生成的标记。

The problem is that I need to render the markup on the server for SEO reasons, but when I call React.renderComponent() it replace the existing markup with the one generated by React.

在React的文档中搜索我发现了这个注释:

Searching in React's docs I found this note:


React.renderComponent()替换你传递的容器节点的内容将来,有可能在不覆盖现有子节点的情况下将组件插入现有DOM节点。

React.renderComponent() replaces the contents of the container node you pass in. In the future, it may be possible to insert a component to an existing DOM node without overwriting the existing children.

此外我不能使用 React.renderComponentToString()生成标记服务器端,因为我的后端在PHP上运行...

Moreover I can't use React.renderComponentToString() to generate the markup server-side because my backend runs on PHP...

是否有任何(即使是hackish)方式来实现当前版本(0.11.2)?

Is there any (even if hackish) way to achieve this with the current release (0.11.2)?

我认为如果可以通过标记生成renderComponentToString ()应该有一种模拟结果的方法吗?

I suppose that if that's possible with markup generated by renderComponentToString() there should be a way to mock that result?

感谢您的任何建议!

推荐答案

您可以查看 https://github.com/reactjs/react-php-v8js

它使用PHP呈现服务器端的UI组件。

It renders react UI components on the server-side with PHP.

实现非常简单,唯一的要求是您需要能够在服务器上设置 V8Js PHP扩展

The implementation is very simple and the only requirement is that you need to be able to setup the V8Js PHP extension on your server.

这篇关于从PHP渲染React组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆