Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@react-facet/deferred-mount

Package Overview
Dependencies
Maintainers
0
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-facet/deferred-mount

  • 0.6.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

@react-facet/deferred-mount

React Facet is a state management for performant game UIs. For more information on how to use this package check the official documentation available at https://react-facet.mojang.com/.

This package allows you to defer the mounting of a component to the next frame. By wrapping components on a big list you can keep the frame time low while everything is mounted.

When the DeferredMountProvider is used, it requires that there is at least one descendent as a DeferredMount or DeferredMountWithCallback, otherwise it will wait forever as deferring.

Example

In the example below it will mount:

  • Mounting: nothing
  • First Frame: First
  • Second Frame: First and Second
  • Third Frame: First, Second and Third

The useIsDeferring hook allows to check what is the status of the deferred mounting (by returning a Facet) so we can, for example, show a spinner.

const SampleComponent = () => {
  const isDeferringFacet = useIsDeferring()

  return (
    <>
      <fast-text text={useFacetMap((isDeferring) => (isDeferring ? 'deferring' : 'done'), [], [isDeferringFacet])} />
      <DeferredMount>
        <div>First</div>
      </DeferredMount>
      <DeferredMount>
        <div>Second</div>
      </DeferredMount>
      <DeferredMount>
        <div>Third</div>
      </DeferredMount>
    </>
  )
}

render(
  <DeferredMountProvider frameTimeBudget={16}>
    <SampleComponent />
  </DeferredMountProvider>,
  document.getElementById('root'),
)

The frameTimeBudget prop allows the tweaking of how much time the library has available to do work on a given frame (by default it targets 60fps).

Deferring Asynchronous Renders

Some components may need to wait some time before they can be considered fully rendered (for example if they are fetching data). For these cases you should use DeferredMountWithCallback with the useNotifyMountComplete hook.

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'),
)

FAQs

Package last updated on 25 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc