How can I make React Portal work with React Hook?

后端 未结 7 2052
感动是毒
感动是毒 2021-01-01 22:05

I have this specific need to listen to a custom event in the browser and from there, I have a button that will open a popup window. I\'m currently using React Portal to open

7条回答
  •  Happy的楠姐
    2021-01-01 22:27

    The chosen/popular answer is close, but it needlessly creates unused DOM elements on every render. The useState hook can be supplied a function to make sure the initial value is only created once:

    const [containerEl] = useState(() => document.createElement('div'));
    

提交回复
热议问题