ReactJS重新渲染/重绘服务器生成的视图

所以我有这个应用程序,让我们说它类似于Facebook在你的墙上张贴的东西,</ p> \ n

一切都在PHP服务器/ Codeigniter框架上呈现并发送到客户端,我需要使用ReactJS呈现这些帖子,因为我在每个帖子中都有很棒的东西</ p> \ n


  1. 赞</ li>
  2. 评论</ li>
  3. 编辑/报告</ li>
    </ ol>

    我不想使用V8JS,因为客户端无法使用php服务器,只需要php脚本即可工作,</ p>

    我有什么想法可以做这些人吗? 非常感谢,</ p>

    可能有一个想法,</ p>

    将帖子加载为服务器呈现,然后执行GET请求并呈现相同的帖子 再次使用ReactJS? 这有用吗?</ p>

    或者有没有办法将所有ReactJS函数和事件附加到服务器呈现的html页面?</ p>
    </ div>

展开原文

原文

So I have this app, Lets say it is similar to Facebook posting something on your wall,

Everything is rendered on a PHP server / Codeigniter Framework and sent to the client, I need these posts to be rendered using ReactJS because I have awesome things I like to do inside each of these post

  1. Like
  2. Comment
  3. Edit / Report

I DO NOT WANT TO USE V8JS, because the client can not play with the php servers, just need the php script to work that's it,

Any thoughts how I could do this guys? Thanks so much,

One Idea is to may be,

Load the posts as server rendered and then do a GET request and render the same posts again using ReactJS? Will that work?

Or is there a way to attach all ReactJS functions and events to a server rendered html page?

1个回答

For React on the client/web browser to attach to an existing DOM structure (that was server rendered for example), it uses an attribute called data-react-checksum which is added to the root DOM element. Calling React.renderComponentToString for example will include this attribute.

<div data-react-checksum="452232529">

When you call renderElement on the same root element, React executes as normal calling render, etc. But, at the end of the render cycle, the data-react-checksum is compared to what was locally computed. If the value matches what was already in the DOM (provided by the server), the Virtual DOM attaches to the existing DOM elements. But to do that, as I mentioned it must match exactly.

Further, you'd need to also include the data-reactid attribute on the DOM elements that would be normally associated with React's virtual DOM.

If you can't execute the React render pipeline on the web server, you should rely on using Ajax to fetch the data. You could render the entire page via PHP, then fetch the data on the client and re-render the page. But, you'll need to decide if that makes sense for your application and users.

I'd suggest you consider just using React for the interactive sections of your web page, and construct those entire on the client as needed.

drwo32555
drwo32555 好吧很酷,然后我将不得不运行nodejs服务器
5 年多之前 回复
dongliugu8843
dongliugu8843 如果你在服务器上生成php中的html,并且你想在react.js中渲染相同的东西,你需要写两次所有内容。 这就是人们在服务器上运行JS的原因,如果你不能这样做,就没有其他的快捷方式了。
5 年多之前 回复
drpsrvu85668
drpsrvu85668 我不明白你的意思。 您需要编写反映PHP代码所创建结构的React组件。 更新jsfiddle中没有任何内容。
5 年多之前 回复
doubinei1457
doubinei1457 如果你看到这个jsfiddle.net/69z2wepo/9249看到html,我怎样才能使用reactJS重新渲染从服务器发送的html? 请更新小提琴:)
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问