1

I am learning React JS and new to it. I have following code to integrate one tap sign in with phone (From Phone.Email) on my website. This button will verify OTP and send back verified number to my website.

<div id="pheIncludedContent"></div>
<script src="https://auth.phone.email/login_automated_v1_2.js"></script>
<script>
  var reqJson='{"success_url":"","client_id":"XXXXXXXXXXXXXXXXX","button_text":"Sign in with Phone","email_notification":"icon","button_position":"left"}';
log_in_with_phone(reqJson);
</script>

I am trying to convert it into React JS code and trying to integrate it on my website. Following is my React JS component code

import React, { useEffect } from 'react';

const SigninPhone = () => {
  useEffect(() => {
    // Create a script element
    const script = document.createElement('script');
    
    // Set the src attribute to the external script URL
    script.src = 'https://auth.phone.email/login_automated_v1_2.js';
    
    // Append the script element to the document body
    document.body.appendChild(script);
    
    // Define the request JSON object
    const reqJson = {
      success_url: '',
      client_id: 'XXXXXXXXXXXXXXXXX',
      button_text: 'Sign in with Phone',
      email_notification: 'icon',
      button_position: 'left'
    };
    
    // Call the log_in_with_phone function with the request JSON object
    window.log_in_with_phone(JSON.stringify(reqJson));

    // Clean up function to remove the script element when the component unmounts
    return () => {
      document.body.removeChild(script);
    };
  }, []); // Empty dependency array to ensure useEffect runs only once

  return <div id="pheIncludedContent"></div>;
};

export default SigninPhone;

But I am getting error like "Uncaught TypeError: window.log_in_with_phone is not a function" log_in_with_phone function is inside https://auth.phone.email/login_automated_v1_2.js this script.

Please help me to solve this problem

1
  • Just keep the script tag, what reason is there to use Javascript to set up the script tag instead of just keeping it on your site? i don't think is fetching the source when you create it using javascript. If you really want to do something like this, I found this question: stackoverflow.com/questions/29531097/… However, I don't see why switching to React would require you to dynamically set the source script Commented Mar 22 at 6:06

1 Answer 1

0

I think the issue is you're trying to use the script before it's loaded. Try this:

const [scriptLoaded, setScriptLoaded] = useState(false);

useEffect(() => {
    // Create a script element
    const script = document.createElement('script');
    
    // Set the src attribute to the external script URL
    script.src = 'https://auth.phone.email/login_automated_v1_2.js';
    
    // Append the script element to the document body
    document.body.appendChild(script);
    
    // Define the request JSON object
    const reqJson = {
      success_url: '',
      client_id: 'XXXXXXXXXXXXXXXXX',
      button_text: 'Sign in with Phone',
      email_notification: 'icon',
      button_position: 'left'
    };

    script.onload = () => {
        setScriptLoaded(true)
        window.log_in_with_phone(JSON.stringify(reqJson));
    }
     


    

    // Clean up function to remove the script element when the component unmounts
    return () => {
      document.body.removeChild(script);
    };
  }, []);

return<>{scriptLoaded && <div id='pheIncludedContent'/>}</>
2
  • I have tried your code. Error is gone but now my button is not loading on <div id="pheIncludedContent"></div>. Commented Mar 22 at 6:25
  • Because the button renders before the script is loaded. I've updated my answer
    – alex87
    Commented Mar 22 at 14:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.