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

[SOLUTION] How to get it working with Next.js #32

Closed
ChrisCates opened this issue Aug 31, 2023 · 1 comment
Closed

[SOLUTION] How to get it working with Next.js #32

ChrisCates opened this issue Aug 31, 2023 · 1 comment

Comments

@ChrisCates
Copy link

ChrisCates commented Aug 31, 2023

For those looking for a way to get this working in Next.js or any SSR based environment. Do the following:

Step 1: Create a null reference to the library.

const [ReactCaptcha, setReactCaptcha] = useState(null as any)

Step 2: Dynamically load module using useLayoutEffect

useLayoutEffect(() => {
    async function loadModule() {
      setReactCaptcha(await import('react-simple-captcha'))
    }
    loadModule()
}, [])

Step 3: have a useState() hook for rendering the Captcha Component.

useLayoutEffect(() => {
    if (ReactCaptcha) {
      setRender(true)
    }
}, [ReactCaptcha])

useEffect(() => {
    if (render) {
      setTimeout(() => {
        ReactCaptcha.loadCaptchaEnginge(6, '#07090e', 'rgb(0, 200, 0)', 'lower')
      }, 250)
    }
}, [render])

Step 4: Render Captcha Component only when it's ready to be rendered.

return (
  <div>
    <div className="captcha">
      {render ? <ReactCaptcha.LoadCanvasTemplate /> : null}
    </div>
  </div>
)
@masroorejaz
Copy link
Owner

Thanks you so much for the solution @ChrisCates

I am referencing your solution in official Read Me of NPM.

Again Thank you so much.

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

2 participants