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

Provide a prop on <Suspense> to let children mount/unmount instead of changing style #18856

Closed
huozhi opened this issue May 7, 2020 · 7 comments

Comments

@huozhi
Copy link
Contributor

huozhi commented May 7, 2020

Feature Request

Currently react render children of Suspense with inline style display: none !important, referenced PR. would like to have a prop like mount or sth else to remount the children when suspended state changed.

Example

const Chid = () => 
   // ... 
   return <div>{data}</div>
<Suspense mount fallback={<span>fallback</span>}>
  <Chid />
</Suspense>

the above code will only render fallback component

<span>fallback</span>

instead of

<span>fallback</span>
<div style="display: none !important;">data</div>
@threepointone
Copy link
Contributor

Could you explain your specific usecase/issue that makes you desire this behaviour?

@huozhi
Copy link
Contributor Author

huozhi commented May 7, 2020

@threepointone I came across this behavior when I was doing snapshot for suspense rendering results with jest. I need to filter out the hidden children and find the actual rendered fallback element to detect if it's suspended.

but I'm wondering there would be some cases that people don't want the nodes just be hidden, remounting can also be a choice. other effects like the order of elements will change, that fallback might not be the only element or can become the second element.

@threepointone
Copy link
Contributor

iirc, the dom elements might be holding state, so it's actually important that they stay in the dom.

It's also a little odd to do visibility checks with snapshots. Consider that some other code, even user code, might have hidden a parent element. Then your test would pass even though it doesn't 'work' as expected. As an immediate workaround, I'd recommend making a helper, something like isVisible() that checks the snapshotted tree. I'd also recommend moving to a 'real' dom for testing (like jsdom) and using regular dom apis for assertions like isVisible() (eg - https://stackoverflow.com/a/21696585)

@eps1lon
Copy link
Collaborator

eps1lon commented May 7, 2020

Sounds like a duplicate of #14536

This is a known quirk with ReactDOM.render (i.e. legacy mode) that will be fixed in blocking (createBlockingRoot) and concurrent mode (createRoot). In the experimental modes react wont trees in an inconsistent state.

It seems like the fix to this will be #15504. Note it requires that your tree is Strict Mode compliant. We can't fix this in legacy mode.

-- #14536 (comment)

@threepointone
Copy link
Contributor

^ He's right, but that would require you to migrate to concurrent mode, which isn't really a recommended option at the moment.

@eps1lon
Copy link
Collaborator

eps1lon commented May 7, 2020

Yeah. To make this more clear: If this is a duplicate of #14536 then this can't be fixed in stable react 16 as per comment from Dan in #14536 (comment).

@huozhi
Copy link
Contributor Author

huozhi commented May 7, 2020

Thanks for clarification @eps1lon 👍. @threepointone work around on testing side make sense, thanks for the help.
close this issue as duplicate.

@huozhi huozhi closed this as completed May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants