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

Deferred Mount feature: DeferredMountWithCallback #34

Merged
merged 2 commits into from
Feb 8, 2022

Conversation

marlonicus
Copy link
Contributor

Overview

The DeferredMount component will wait for an initial render before marking the render as complete. This works for simple components, but falls apart for components that load content asynchronously.

This PR introduces a new component and hook to handle those cases: DeferredMountWithCallback and useNotifyMountComplete.

Implementation

Originally, the DeferredMountProvider would loop through a queue of any deferred children and call a trigger function that causes them to render; trying to render as many as possible within a given time budget.

As an addition to this, it will now check to see if that trigger function returns a callback which is then used to pause the processing of the queue until the callback is run.

Example

const DelayedComponent = () => {
  const notifyMountComplete = useNotifyMountComplete()
  const [data, setData] = useState()

  useEffect(() => {
    fetch('mock-api').then((data) => {
      setData(data)
      notifyMountComplete()
    })
  }, [notifyMountComplete])

  return <div>{data}</div>
}

render(
  <DeferredMountProvider>
    <DeferredMountWithCallback>
      <DelayedComponent />
    </DeferredMountWithCallback>
  </DeferredMountProvider>,
  document.getElementById('root'),
)

@marlonicus marlonicus changed the title Deferred Rendering feature: DeferredMountWithCallback Deferred Mount feature: DeferredMountWithCallback Feb 8, 2022
Copy link
Contributor

@creativecreature creativecreature left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great description and I really like the test 👍

@xaviervia xaviervia enabled auto-merge (squash) February 8, 2022 09:49
@xaviervia xaviervia merged commit 0c5cc8e into main Feb 8, 2022
@xaviervia xaviervia deleted the deferred-mount-with-callback branch February 8, 2022 09:51
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

Successfully merging this pull request may close these issues.

None yet

3 participants