dqh1992 2015-05-29 11:00
浏览 116

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

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条回答

  • doushuhuai7247 2015-05-29 12:18
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器