Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Box height doesn't reset on re-render #228

Closed
colinking opened this issue Sep 16, 2019 · 7 comments
Closed

Box height doesn't reset on re-render #228

colinking opened this issue Sep 16, 2019 · 7 comments
Labels

Comments

@colinking
Copy link

colinking commented Sep 16, 2019

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!

@vadimdemedes
Copy link
Owner

Could you try out #232 and let me know if it fixes the issue for you?

@colinking
Copy link
Author

Put this example in colinking/ink-testing, so it can be run like so:

$ git checkout https://github.com/colinking/ink-testing.git
$ npm i
$ npx ts-node src/height.tsx

Just tried running it with the latest version from your branch, and it appears that the CLI exits without waiting. For example:

// "ink": "^2.3.0",
$ npx ts-node src/height.tsx
1
2
3

$ 
// "ink": "vadimdemedes/ink#experimental-reconciler",
$ npx ts-node src/height.tsx
Loading...


$ 

@vadimdemedes
Copy link
Owner

Yeah, I had the exact same problem few days ago. Replacing useEffect with useLayoutEffect has solved it for me. Check out #147 (comment). 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.

@vadimdemedes
Copy link
Owner

I'm not sure why original reconciler/renderer doesn't have the same problem, I wonder if it's the slowness of building new output every frame. New reconciler is significantly faster in that regard, so it could be that.

@vadimdemedes
Copy link
Owner

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.

@vadimdemedes
Copy link
Owner

Fixed via e5b7569 for non-experimental mode. Will keep this issue open until release.

@vadimdemedes
Copy link
Owner

Fixed in 2.5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants