最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React 18: how to force synchronous rendering? - Stack Overflow

programmeradmin1浏览0评论

After upgrading to React 18, I am having issues with Leaflet popup rendering.

It seems the new rendering function has bee asynchronous, which breaks Leaflet popup display (because Leaflet needs to pute the size of the popup for positioning, but now it gets an empty content since React has not yet rendered the popup).

Before (working as expected):

marker.bindPopup(() => {
  var div = document.createElement('div');
  ReactDOM.render(<MyPopup />);
  console.log(div.innerHTML); // <div>[...]</div>
  return div;
});

With React 18 (positioning broken):

marker.bindPopup(() => {
  var div = document.createElement('div');
  createRoot(div).render(<MyPopup />);
  console.log(div.innerHTML); // empty string!
  return div;
});

Is there any way to force React 18 to render the popup before returning the div?

After upgrading to React 18, I am having issues with Leaflet popup rendering.

It seems the new rendering function has bee asynchronous, which breaks Leaflet popup display (because Leaflet needs to pute the size of the popup for positioning, but now it gets an empty content since React has not yet rendered the popup).

Before (working as expected):

marker.bindPopup(() => {
  var div = document.createElement('div');
  ReactDOM.render(<MyPopup />);
  console.log(div.innerHTML); // <div>[...]</div>
  return div;
});

With React 18 (positioning broken):

marker.bindPopup(() => {
  var div = document.createElement('div');
  createRoot(div).render(<MyPopup />);
  console.log(div.innerHTML); // empty string!
  return div;
});

Is there any way to force React 18 to render the popup before returning the div?

Share Improve this question edited Nov 17, 2024 at 6:33 Drew Reese 204k18 gold badges244 silver badges273 bronze badges asked Apr 23, 2022 at 17:46 piero-la-lunepiero-la-lune 3012 silver badges6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

I just found another solution that seems to be working, and better suited to a production environment: flushSync. In the React documentation, it is not mentioned for this purpose, only to opt out of automatic batching.

I still not sure if this is the correct way to do it, or if it will break in a future update.

marker.bindPopup(() => {
  var div = document.createElement('div');
  const root = createRoot(div);

  flushSync(() => {
    root.render(<MyPopup />);
  });

  console.log(div.innerHTML); // <div>[...]</div>
  return div;
});

Didn't tried on your scenario but maybe act could work https://reactjs/docs/testing-recipes.html#act

Remember to import act from react-dom/test-utils

marker.bindPopup(() => {
   var div = document.createElement('div');
   const root = createRoot(div);

   act(() => {
     root.render(<MyPopup />);
   });

   return div;
});
发布评论

评论列表(0)

  1. 暂无评论