Converting this javascript code to React JS

I am integrating one tap sign in with phone on my website.
I am trying to convert following javascript code to React JS.

<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>

My React JS code is:

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 (Including from Phone Email)
    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 Uncaught TypeError: window.log_in_with_phone is not a function. Please help.

hello and welcome to fcc forum :slight_smile:

  • that supposedly should show that error!!
  • you can either run without using “window”, or if its need to be a “button” clicked function then run this as “click event” instead

happy coding :slight_smile:

Did you look at the docs?