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

@enrise/usepromiseloader

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@enrise/usepromiseloader

A react hook for nextjs that enables you to show a loader when data is fetched with getInitialProps in SPA mode

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status

usePromiseLoader

In NextJs a page will only be rendered when everything in getInitialProps is loaded. This hook will allow you to directly render and show a loader while the data is being loaded. Once the data is loaded the page will re-render and show the data.

How to use

import usePromiseLoader from '@enrise/usepromiseloader'

type TestPageProps = {
  data: string
};

const TestPage: NextPage<TestPageProps> = ({ data }) => {
  const [resolvedData, loading] = usePromiseLoader(data, '');

  return (
    <div>
      {loading && 'loading...'}
      {!loading && resolvedData}
    </div>
  )
}

TestPage.getInitialProps = async () => {
  const data = process.browser ? getData() : await getData();
  return { data };
}

As you can see in the getInitialProps the promise from getData is directly returned when it is ran in a browser. The usePromiseLoader hook will make sure that the data is correctly loaded when the promise is resolved.

Keywords

FAQs

Package last updated on 16 Apr 2020

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