【发布时间】:2020-11-14 22:43:48
【问题描述】:
e.prompt() 派生自 beforeinstallprompt 导致无限 useEffect 循环。
在beforeinstallprompt 上,handle_storePrompt() 将 showButton 设置为 true (<button /> "display:block")。 OnClick,handle_prompt() 将 showButton 设置为 false (<button /> "display:none")。在 prompt() 解决后,beforeinstallprompt 会被触发,从而导致 handle_storePrompt() 再次运行。
组件
const [showButton, setShowButton] = useState(false)
const [prompt, setPrompt] = useState()
useEffect(() => {
const handle_storePrompt = e => {
e.preventDefault()
console.log('Action Triggered')
setPrompt(e)
setShowButton(true)
}
window.addEventListener('beforeinstallprompt', e =>
handle_storePrompt(e)
)
return _ => {
window.removeEventListener('beforeinstallprompt', e =>
handle_storePrompt(e)
)
}
}, [])
const handle_prompt = _ => {
setShowButton(false)
prompt.prompt()
}
return (
<button className={showButton ? Styles.show : ''} onClick={handle_prompt}>
<small>Click to Install</small>
</button>
)
【问题讨论】:
标签: reactjs react-hooks progressive-web-apps gatsby