1.React.renderToString 函数,  参数是组件,返回一个字符串

<!DOCTYPE html>
<html>
    <head>
        <title>React JS</title>
        <script src="../build_0.13/react.js"></script>
        <script src="../build_0.13/JSXTransformer.js"></script>
        <script src="../build_0.13/react-with-addons.min.js"></script>
        <style type="text/css">
            .example-enter{color:red;}
            .example-active{color:green;}
        </style>
        
    </head>
    <body>
        <div  >&nbsp;</div>
        <script type="text/jsx">
            var MyComponent = React.createClass({
                    render:function(){
                        return (
                            <div>Hello World!</div>
                        );
                    }    
                });
            var world = React.renderToString(<MyComponent />);
            alert(world);
            console.log(world);
        </script>
    </body>
</html>

React(0.13)  服务端渲染的两个函数

2.另一个服务端渲染函数: React.renderToStaticMarkup ,他没有data属性

<!DOCTYPE html>
<html>
    <head>
        <title>React JS</title>
        <script src="../build_0.13/react.js"></script>
        <script src="../build_0.13/JSXTransformer.js"></script>
        <script src="../build_0.13/react-with-addons.min.js"></script>
        <style type="text/css">
            .example-enter{color:red;}
            .example-active{color:green;}
        </style>
        
    </head>
    <body>
        <div  >&nbsp;</div>
        <script type="text/jsx">
            var MyComponent = React.createClass({
                    render:function(){
                        return (
                            <div>Hello World!</div>
                        );
                    }    
                });
            //var world = React.renderToString(<MyComponent />);
            var world = React.renderToStaticMarkup(<MyComponent />);
            alert(world);
            console.log(world);
        </script>
    </body>
</html>

 

 

React(0.13)  服务端渲染的两个函数

两者在什么时候使用呢?

当且仅当你不打算在客户端渲染这个React Component时,才应该选择使用React.renderToStaticMarkup函数,如:

    •   生成html电子邮件
    •   通过HTML到PDF的转化生成PDF
    •   组件测试

 

大多情况下 ,我们都使用React.renderToString()来进行服务端的渲染。

相关文章:

  • 2022-01-18
  • 2021-12-05
  • 2021-09-29
  • 2021-10-05
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
猜你喜欢
  • 2022-12-23
  • 2021-07-07
  • 2021-11-03
  • 2021-11-10
  • 2021-06-03
  • 2022-02-28
相关资源
相似解决方案