Is it ok to use ReactDOMServer.renderToString in the browser in areas where React isn't directly managing the DOM?

前端 未结 3 1548
暖寄归人
暖寄归人 2020-12-25 10:29

I\'m working on an app using Leaflet (via react-leaflet). Leaflet directly manipulates the DOM. The react-leaflet library doesn\'t change that, it just gives you React compo

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-25 11:01

    As Thomas already said, yes, you can use renderToString on the client. Just to be clear though, you will need to import ReactDOMServer on the client, which may seem counter-intuitive but appears to be correct. Example (on the client):

    import React from 'react';
    import ReactDOMServer from 'react-dom/server';
    
    const MyComp = (props) => {
      const html = ReactDOMServer.renderToString(
    {someFunc(props)}
    ); // do something with your html, then return
    ; };

提交回复
热议问题