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

Server-side validation and module dependency #440

Open
omeralpi opened this issue Feb 24, 2024 · 6 comments
Open

Server-side validation and module dependency #440

omeralpi opened this issue Feb 24, 2024 · 6 comments

Comments

@omeralpi
Copy link

omeralpi commented Feb 24, 2024

Hello,

I've encountered an issue when using server actions with Next.js and attempting to validate phone numbers on the server side.

Currently, the documentation/code suggests using:

import { isValidPhoneNumber } from "react-phone-number-input";

However, this import is client-side oriented and might not be suitable for server-side validations due to its dependency on the react-phone-number-input package, which is primarily designed for client-side React applications.

As an alternative, it would be much better if there were a usage like this for server-side validations within Next.js server actions.

import { isValidPhoneNumber } from 'react-phone-number-input/libphonenumber';

or

import { isValidPhoneNumber } from 'react-phone-number-input/libphonenumber';
import { isValidPhoneNumber } from 'react-phone-number-input/core/libphonenumber';
@catamphetamine
Copy link
Owner

catamphetamine commented Feb 24, 2024 via email

@omeralpi
Copy link
Author

omeralpi commented Feb 24, 2024

I forgot to include the details. Here you go;

Codesandbox example

Screen.Recording.2024-02-24.at.14.21.04.mov

Error message;

ReactServerComponentsError:

You're importing a component that needs useRef. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.

    ,-[/node_modules/.pnpm/input-format@0.3.9/node_modules/input-format/modules/react/Input.js:7:1]
 10 | import React, { useCallback, useRef } from 'react'; 

Hello. I don’t think that there could be any issues with using the package on the server side.

@catamphetamine
Copy link
Owner

Hmm, I see. Well, in that case just import the validation functions from libphonenumber-js package because all those functions are simply re-exported from that package.

libphonenumber-js package is a dependency of react-phone-number-input and is automatically installed.

@catamphetamine
Copy link
Owner

Or, as a workaround, you could do the thing it tells in the error message:

none of its parents are marked with "use client"

I.e. one could simply mark a parent component *.js file with "use client" at the top.

@omeralpi
Copy link
Author

omeralpi commented Feb 25, 2024

Or, as a workaround, you could do the thing it tells in the error message:

none of its parents are marked with "use client"

I.e. one could simply mark a parent component *.js file with "use client" at the top.

Thank you for your response.

The problem starts there. In Server Actions, 'use client' cannot be used. The file from which we import the function, such as 'isValidPhoneNumber', should not contain hooks like useRef.

What I mean next is, I believe this issue can be resolved if we import such validation functions separately, for instance, something like 'react-phone-number-input/libphonenumber.' 🙏
You, as the creators of the package, would know better! 🫡

Hmm, I see. Well, in that case just import the validation functions from libphonenumber-js package because all those functions are simply re-exported from that package.

libphonenumber-js package is a dependency of react-phone-number-input and is automatically installed.

The challenge here is to manage the versions and ensure compatibility between these dependencies. 🥹

@ahkhanjani
Copy link

Hmm, I see. Well, in that case just import the validation functions from libphonenumber-js package because all those functions are simply re-exported from that package.

Perhaps not re-exporting the utilities and having to install the dependency would solve similar issues.

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

3 participants