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

Add api exposing a hook-based way to load a module #976

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

edkimmel
Copy link
Contributor

Summary

Adds loadable.hook and lazy.hook as additional ways to load a dynamic module via @Loadable

A common use case is to use some large third party library as part of a hook in first party code. By exposing a hook-based API in @Loadable, it becomes trivial to dynamically load that library module.

const useThirdPartyAuthSdk = loadable.hook(() => 'some-large-sdk')
...

const useUserSignIn = () => {
  // 'some-large-sdk' is not loaded until this hook is ran
  const thirdPartyAuthSdk = useThirdPartyAuthSdk()
  const signin = React.useCallback(creds => {
    if (!thirdPartyAuthSdk) throw new Error('Auth SDK used before ready')
    thirdPartyAuthSdk.initiateSignin(creds)
  }, [thirdPartyAuthSdk])
  return {
    ready: !!thirdPartyAuthSdk,
    signin
  }
}

Test plan

loadableHook.test.js has been created mirroring loadable.test.js where applicable.
An example for the hook api was created in the repository.

@theKashey theKashey self-requested a review August 26, 2023 01:14
@theKashey
Copy link
Collaborator

Amazing job and quite interesting implementation. Big kudos for comprehensive tests.

However, let me be clear - I am going to add this PR to the list of all other things one way or another assigned to me and I cannot provide you any ETA for actual resolution. My presence in GitHub lands is quite limited nowadays.

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.

2 participants