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.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮