加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

React 16使用了Portals时,Server Side Render碰到的问题

发布时间:2020-12-15 06:47:18 所属栏目:百科 来源:网络整理
导读:最近发布的React 16支持Portals(https://facebook.github.io/react/docs/portals.html),也就是支持将子组件render到其它任何地方。Vuejs也支持类似的功能,见https://github.com/LinusBorg/portal-vue。 昨天尝试了下Portals功能,demo如下 import React,{Co

最近发布的React 16支持Portals(https://facebook.github.io/react/docs/portals.html),也就是支持将子组件render到其它任何地方。Vuejs也支持类似的功能,见https://github.com/LinusBorg/portal-vue。

昨天尝试了下Portals功能,demo如下

import React,{Component} from 'react';
import ReactDOM from 'react-dom';
import jsdom from 'jsdom';

const { JSDOM } = jsdom;
const dom = new JSDOM(`
    <!DOCTYPE html>
    <html>
        <body>
            <div id="app"></div>
            <div id="domNode"></div>
        </body>
    </html>
`);
const document = dom.window.document;
const htmlNode = document.querySelector("html");
const appNode = document.querySelector("#app");
const domNode = document.querySelector("#domNode");

class Portals extends Component {
    render() {
        return ReactDOM.createPortal(
            this.props.children,domNode,);
    }
}

ReactDOM.render(
    <Portals>
        <h1>hello</h1>
        <span>Hello React 16.</span>
    </Portals>,appNode
);
console.log('htmlNode -> ',htmlNode.outerHTML);

输出结果如下:

htmlNode ->  <html><head></head><body>
            <div id="app"></div>
            <div id="domNode"><h1>hello</h1><span>Hello React 16.</span></div>


</body></html>

可以看到使用了Portals后,<Portals>组件的children render到domNode中了,而不是render到app节点中。

下面来看下在使用服务端渲染时的代码:

import React,{Component} from 'react';
import ReactDOM from 'react-dom';
import ReactDOMServer from 'react-dom/server';
import jsdom from 'jsdom';

const { JSDOM } = jsdom;
const dom = new JSDOM(`
    <!DOCTYPE html>
    <html>
        <body>
            <div id="app"></div>
            <div id="domNode"></div>
        </body>
    </html>
`);
const document = dom.window.document;
const htmlNode = document.querySelector("html");
const domNode = document.querySelector("#domNode");

class Portals extends Component {
    render() {
        return ReactDOM.createPortal(
            this.props.children,);
    }
}

const str = ReactDOMServer.renderToString(
    <Portals>
        <h1>hello</h1>
        <span>Hello React 16.</span>
    </Portals>
);
console.log('str',str);
console.log('htmlNode -> ',htmlNode.outerHTML);

代码主体功能没差别,主要是ReactDOM.render改成了ReactDOMServer.renderToString,

但这次render没有成功,具体错误信息如下:

D:work_reactreact16-demonode_modules.npminstallfbjs.8.16fbjslibinvariant.js:49
    throw error;
    ^

Invariant Violation: Objects are not valid as a React child (found: object with keys {$$typeof,key,children,containerInfo,implementation}). If you
 meant to render a collection of children,use an array instead.
    in Portals
    at invariant (D:work_reactreact16-demonode_modules.npminstallfbjs.8.16fbjslibinvariant.js:42:15)
    at traverseAllChildrenImpl (D:work_reactreact16-demonode_modules.npminstallreact16.0.0reactcjsreact.development.js:830:7)
    at traverseAllChildren (D:work_reactreact16-demonode_modules.npminstallreact16.0.0reactcjsreact.development.js:858:10)
    at mapIntoWithKeyPrefixInternal (D:work_reactreact16-demonode_modules.npminstallreact16.0.0reactcjsreact.development.js:934:3)
    at toArray (D:work_reactreact16-demonode_modules.npminstallreact16.0.0reactcjsreact.development.js:981:3)
    at ReactDOMServerRenderer.render (D:work_reactreact16-demonode_modules.npminstallreact-dom16.0.0react-domcjsreact-dom-server.node.developme
nt.js:2757:26)
    at ReactDOMServerRenderer.read (D:work_reactreact16-demonode_modules.npminstallreact-dom16.0.0react-domcjsreact-dom-server.node.development
.js:2722:19)
    at Object.renderToString (D:work_reactreact16-demonode_modules.npminstallreact-dom16.0.0react-domcjsreact-dom-server.node.development.js:29
80:25)
    at Object.<anonymous> (D:/work_react/react16-demo/demo/portalsServerSide.js:29:28)
    at Module._compile (module.js:571:32)

还没有找到具体的原因。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读