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.

`Box` `height` doesn't reset on re-render

See original GitHub issue

It appears that the height property might be updated when re-rendering a component, though I may be misunderstanding something!

Here’s a quick example:

// We're waiting for an async process, so we'll want a loading state.
const [results, setResults] = useState([])
useEffect(() => {
  loadResults().then(output => {
    setResults(output)
  })
}, [])

// Loading state if we're still waiting on the results
if (results.length === 0) {
  return (
    <Box height={3}>
      <Color grey>Loading...</Color>
    </Box>
  )
}

// Once the results load, show the list:
return (
  <Box flexDirection="column">
    <Color>1</Color>
    <Color>2</Color>
    <Color>3</Color>
    <Color>4</Color>
    <Color>5</Color>
  </Box>
)

I would expect to see all five numbers printed out, but only see the first three:

$ yarn dev
1
2
3

When run with debug=true:

PS – would be nice if debug mode added in a newline in between renders!

$ yarn dev --debug
Loading...

1
2
31
2

Note: I just swapped height out with marginTop/marginBottom here and it works fine!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
vadimdemedescommented, Sep 23, 2019

I can’t believe I just spent an entire day debugging React internals and all it was is just out-dated react-reconciler dependency that I had. Kill me now.

1reaction
vadimdemedescommented, Sep 22, 2019

Yeah, I had the exact same problem few days ago. Replacing useEffect with useLayoutEffect has solved it for me. Check out https://github.com/vadimdemedes/ink/pull/147#issuecomment-531598933. TLDR is useEffect doesn’t run soon enough after Ink renders a frame, so process exits before setTimeout is executed in your code. useLayoutEffect however, ensures to run the effect immediately after render.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flex-box items don't re-render correctly on chrome
A js workaround is to force the layout to re-render by quickly changing the items padding by 1px back and forth, but I...
Read more >
Re-render a React Component on Window Resize | Pluralsight
Now we have set up some internal state, dimensions , that has height and width properties. Inside handleResize , we no longer simply...
Read more >
resetting form values inside lit elment doesnt't trigger re-render
Description I have a form, having 3 input elements, I want to set it to null value (reset), after the form is submitted,...
Read more >
Forcing state reset on a React component by using the key prop
One solution is to define componentWillReceiveProps , check if activeChat prop has changed and if so reset the message field manually. This solution...
Read more >
Just Say No to Excessive Re-Rendering in React - GrapeCity
Typically, React performs well out of the box using techniques to reduce ... in a consequent re-render of the MovieDetails which does not...
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