question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chrome tab freezes with useLayoutEffect

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? Browser tab freezes

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

I was playing around with hooks and just for the heck of it changed useEffect to useLayoutEffect and the browser crashed. After playing around I reduced it to this minimal code and it still crashes. To reproduce the bug, simply change useEffect to useLayoutEffect

import React, { useState, useEffect } from 'react'
import ReactDOM from 'react-dom'

const getProducts = () => {
  return Promise.resolve([
    { id: 1, name: 'Product A' },
    { id: 2, name: 'Product B' }
  ])
}

const App = () => {
  const [products, setProducts] = useState([])

  useEffect(() => {
    getProducts().then(p => {
      setProducts(p)
    })
  })

  return (
    <div>
      {products.map(product => (
        <div key={product.id}>{product.name}</div>
      ))}
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('root'))

https://codesandbox.io/s/zl9vz5yxol

When you make the change if you close the browser tab fast enough (it stalls for a few seconds for me but will close). But if I wait too long I have to force quit chrome

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React/Dom: 16.7.0-alpha.2 Chrome: Version 71.0.3578.98 (Official Build) (64-bit) macOS Mojave 10.14.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
bradwestfallcommented, Dec 27, 2018

oops, my fault on the second arg. Also I can see with this being a loop why it might be difficult to mitigate. Maybe some docs on it would be good. Since it’s not an explicit looking infinite loop, I’m sure there will be some others who fall into this on accident

0reactions
heyimalexcommented, Dec 27, 2018

The issue is that you’re causing an infinite loop. You almost fixed the code, but you actually passed the array parameter to getProducts.then instead of useLayoutEffect. Here’s a fixed sandbox.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Browser tab freezes after using useState hook in React
The issue is with your useEffect useEffect(() => { const loggedUserJSON = window.localStorage.getItem('loggedBlogappUser') if ...
Read more >
Chrome freezes when closing youtube video tabs
The main reason why Chrome freezes could be that something is trying to resolve it's process even though you've closed the tab. Two...
Read more >
useMemo - React Docs
A JavaScript library for building user interfaces.
Read more >
How to Fix It When Chrome Keeps Freezing - Lifewire
This simple troubleshooting step often solves the problem. Close all Chrome tabs and windows, wait a few minutes, and reopen Chrome. If Chrome ......
Read more >
Untitled
useLayoutEffect (e,t)}function $(e,t){return W(). ... hasOwnProperty(o)){var a=r[o];if(o===ig)a&&Object.freeze(a),Vt(t,a);else if(o===Qv){var s=a?a[og]:void ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found