How can I make React Portal work with React Hook?

后端 未结 7 2046
感动是毒
感动是毒 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条回答
  •  离开以前
    2021-01-01 22:20

    The issue is: a new div is created on every render, just create the div outside render function and it should work as expected,

    const containerEl = document.createElement('div')
    const PopupWindowWithHooks = props => {
       let externalWindow = null
       ... rest of your code ...
    

    https://codesandbox.io/s/q9k8q903z6

提交回复
热议问题