JSON object vs window variable for passing server-side rendered initial state using reactjs

感情迁移 提交于 2019-12-22 14:18:05

问题


In an isomorphic application with Reactjs, you need to pass the identical initial state that was rendered on the server down to the client (which will then 'rehydrate' the app with event bindings and such).

I've seen two approaches to passing this initial state down--

Setting a global variable to the window:

<script>
window.initialState = {{JSON.stringify(initialState)}} ;
</script>

Or passing it as a JSON object:

<script id="initial-state" type="application/json"> {{JSON.stringify(initialState)}}</script>

Both are easily retrievable from anywhere in the application. Are there any advantages to using one over the other?


回答1:


The latter avoids a global variable and the former avoids a DOM lookup. I'd go with the former, just because it requires less code.

One concern is if you have </script in your JSON it could allow injection or accidental error. To prevent this you can replace < with \u003c.

<script>
window.initialState = {{
    JSON.stringify(initialState).replace(/</g, '\\u003c')
}}; 
</script>



回答2:


I like to create a start function that kicks things off on the server and in the browser. On the browser side I render that initial state object as an argument:

<script type="text/javascript">
    var app = new App();
    document.addEventListener('DOMContentLoaded', function(e) {
        document.removeEventListener('DOMContentLoaded');

        app.start({{JSON.stringify(initialState)}});
    });
</script>

In the start function, I have something like this for the browser:

App.prototype.start = function(initState) {
    React.render(RootComponent(initState), document.getElementById('container'));
}

In this case start() doesn't do much, but in a full implementation I would also handle the server-side rendering here. Most of my ideas for this came from this talk and examples: https://github.com/zertosh/ssr-demo-kit



来源:https://stackoverflow.com/questions/27669648/json-object-vs-window-variable-for-passing-server-side-rendered-initial-state-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!