<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Survey123 Javascript Embed with react in ArcGIS Survey123 Questions</title>
    <link>https://community.esri.com/t5/arcgis-survey123-questions/survey123-javascript-embed-with-react/m-p/1601978#M61972</link>
    <description>&lt;P&gt;I was able to get this (sort of) working using Ionic and React - using Next might prove to be a bit more challenging though with default SSR and all that.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const SurveyPage: React.FC = () =&amp;gt; {
  const containerRef = useRef&amp;lt;HTMLDivElement&amp;gt;(null);
  const { isSignedIn, portal } = useAuth();

  useEffect(() =&amp;gt; {
    // Run script loading and survey initialization only when signed in.
    if (!isSignedIn) return;

    // Function to load the Survey123 script dynamically
    const loadSurveyScript = () =&amp;gt; {
      return new Promise&amp;lt;void&amp;gt;((resolve, reject) =&amp;gt; {
        // If already loaded, resolve immediately.
        if ((window as any).Survey123WebForm) {
          resolve();
          return;
        }
        const script = document.createElement('script');
        script.src="https://survey123.arcgis.com/api/jsapi/";
        script.onload = () =&amp;gt; resolve();
        script.onerror = () =&amp;gt; reject(new Error("Failed to load Survey123 API"));
        document.head.appendChild(script);
      });
    };

    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    let surveyInstance: any;
    loadSurveyScript()
      .then(() =&amp;gt; {
        // Ensure the container exists and the Survey123WebForm global is available
        if (containerRef.current &amp;amp;&amp;amp; (window as any).Survey123WebForm) {
          // Clear the container to avoid duplicate surveys
          containerRef.current.innerHTML = "";
          surveyInstance = new (window as any).Survey123WebForm({
            clientId: ArcGISConfig.auth.clientId,
            container: containerRef.current,
            itemId: "******",
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            //@ts-expect-error
            token: portal?.credential.token,
          });
        }
      })
      .catch((error) =&amp;gt; {
        console.error("Survey123 script failed to load:", error);
      });

    // Cleanup on unmount
    return () =&amp;gt; {
      if (containerRef.current) {
        containerRef.current.innerHTML = "";
      }
    };
  }, [isSignedIn, portal]);

  return (
    &amp;lt;IonPage&amp;gt;
      &amp;lt;IonHeader&amp;gt;
        &amp;lt;IonToolbar&amp;gt;
          &amp;lt;IonButtons slot="start"&amp;gt;
            &amp;lt;IonMenuButton /&amp;gt;
          &amp;lt;/IonButtons&amp;gt;
          &amp;lt;IonTitle&amp;gt;Survey&amp;lt;/IonTitle&amp;gt;
        &amp;lt;/IonToolbar&amp;gt;
      &amp;lt;/IonHeader&amp;gt;

      &amp;lt;IonContent fullscreen&amp;gt;

        {/* Container for the Survey123 form */}
        {isSignedIn &amp;amp;&amp;amp; &amp;lt;div ref={containerRef} id="formDiv" style={{ padding: '16px', height: '100vh' }} /&amp;gt;}
      &amp;lt;/IonContent&amp;gt;
    &amp;lt;/IonPage&amp;gt;
  );
};

export default SurveyPage;&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;Have you found a better solution?&amp;nbsp; I don't really like how 'non-Reactive' this is.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Aidan&lt;/P&gt;</description>
    <pubDate>Wed, 02 Apr 2025 19:09:13 GMT</pubDate>
    <dc:creator>AidanDonnelly</dc:creator>
    <dc:date>2025-04-02T19:09:13Z</dc:date>
    <item>
      <title>Survey123 Javascript Embed with react</title>
      <link>https://community.esri.com/t5/arcgis-survey123-questions/survey123-javascript-embed-with-react/m-p/1250936#M47056</link>
      <description>&lt;P&gt;Hello All,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking to create a custom web application with the ArcGIS 4.x JS API with React. I would like to embed a Survey123 form to collect data from Users similar to the Survey Widget in Experience Builder.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to integrate the Survey123 API as shown here:&amp;nbsp;&lt;A href="https://developers.arcgis.com/survey123/guide/embed-a-survey-using-javascript/#configure-a-survey-in-arcgis-experience-builder" target="_self"&gt;https://developers.arcgis.com/survey123/guide/embed-a-survey-using-javascript/#configure-a-survey-in-arcgis-experience-builder&amp;nbsp;&lt;/A&gt;with this&amp;nbsp;repo using the "@arcgis/core" with React and Vite?&amp;nbsp;&lt;A href="https://github.com/Esri/jsapi-resources/tree/main/esm-samples/jsapi-react" target="_self"&gt;https://github.com/Esri/jsapi-resources/tree/main/esm-samples/jsapi-react&amp;nbsp;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or should I approach this a different way? Ideally I am looking to have the form load conditionally (i.e only when 1 layer is clicked on). I also am trying to pass(some) data from the clicked on layer to the Survey form.&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the Best,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Andrew&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2023 18:35:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-survey123-questions/survey123-javascript-embed-with-react/m-p/1250936#M47056</guid>
      <dc:creator>ankoure_Cambridge</dc:creator>
      <dc:date>2023-01-24T18:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: Survey123 Javascript Embed with react</title>
      <link>https://community.esri.com/t5/arcgis-survey123-questions/survey123-javascript-embed-with-react/m-p/1541575#M59305</link>
      <description>&lt;P&gt;I'm trying to do something similar.&amp;nbsp; I'm actually trying to integrate the S123 API with this newer sample by&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/7384"&gt;@ReneRubalcava&lt;/a&gt;:&amp;nbsp;&lt;A href="https://github.com/odoe/ds2024-arcgis-map-comps-next/" target="_blank"&gt;https://github.com/odoe/ds2024-arcgis-map-comps-next/&lt;/A&gt;&amp;nbsp;.&amp;nbsp; Were you able to figure this out?&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 18:33:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-survey123-questions/survey123-javascript-embed-with-react/m-p/1541575#M59305</guid>
      <dc:creator>bradMaps</dc:creator>
      <dc:date>2024-09-23T18:33:50Z</dc:date>
    </item>
    <item>
      <title>Re: Survey123 Javascript Embed with react</title>
      <link>https://community.esri.com/t5/arcgis-survey123-questions/survey123-javascript-embed-with-react/m-p/1601978#M61972</link>
      <description>&lt;P&gt;I was able to get this (sort of) working using Ionic and React - using Next might prove to be a bit more challenging though with default SSR and all that.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const SurveyPage: React.FC = () =&amp;gt; {
  const containerRef = useRef&amp;lt;HTMLDivElement&amp;gt;(null);
  const { isSignedIn, portal } = useAuth();

  useEffect(() =&amp;gt; {
    // Run script loading and survey initialization only when signed in.
    if (!isSignedIn) return;

    // Function to load the Survey123 script dynamically
    const loadSurveyScript = () =&amp;gt; {
      return new Promise&amp;lt;void&amp;gt;((resolve, reject) =&amp;gt; {
        // If already loaded, resolve immediately.
        if ((window as any).Survey123WebForm) {
          resolve();
          return;
        }
        const script = document.createElement('script');
        script.src="https://survey123.arcgis.com/api/jsapi/";
        script.onload = () =&amp;gt; resolve();
        script.onerror = () =&amp;gt; reject(new Error("Failed to load Survey123 API"));
        document.head.appendChild(script);
      });
    };

    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    let surveyInstance: any;
    loadSurveyScript()
      .then(() =&amp;gt; {
        // Ensure the container exists and the Survey123WebForm global is available
        if (containerRef.current &amp;amp;&amp;amp; (window as any).Survey123WebForm) {
          // Clear the container to avoid duplicate surveys
          containerRef.current.innerHTML = "";
          surveyInstance = new (window as any).Survey123WebForm({
            clientId: ArcGISConfig.auth.clientId,
            container: containerRef.current,
            itemId: "******",
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            //@ts-expect-error
            token: portal?.credential.token,
          });
        }
      })
      .catch((error) =&amp;gt; {
        console.error("Survey123 script failed to load:", error);
      });

    // Cleanup on unmount
    return () =&amp;gt; {
      if (containerRef.current) {
        containerRef.current.innerHTML = "";
      }
    };
  }, [isSignedIn, portal]);

  return (
    &amp;lt;IonPage&amp;gt;
      &amp;lt;IonHeader&amp;gt;
        &amp;lt;IonToolbar&amp;gt;
          &amp;lt;IonButtons slot="start"&amp;gt;
            &amp;lt;IonMenuButton /&amp;gt;
          &amp;lt;/IonButtons&amp;gt;
          &amp;lt;IonTitle&amp;gt;Survey&amp;lt;/IonTitle&amp;gt;
        &amp;lt;/IonToolbar&amp;gt;
      &amp;lt;/IonHeader&amp;gt;

      &amp;lt;IonContent fullscreen&amp;gt;

        {/* Container for the Survey123 form */}
        {isSignedIn &amp;amp;&amp;amp; &amp;lt;div ref={containerRef} id="formDiv" style={{ padding: '16px', height: '100vh' }} /&amp;gt;}
      &amp;lt;/IonContent&amp;gt;
    &amp;lt;/IonPage&amp;gt;
  );
};

export default SurveyPage;&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;Have you found a better solution?&amp;nbsp; I don't really like how 'non-Reactive' this is.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Aidan&lt;/P&gt;</description>
      <pubDate>Wed, 02 Apr 2025 19:09:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-survey123-questions/survey123-javascript-embed-with-react/m-p/1601978#M61972</guid>
      <dc:creator>AidanDonnelly</dc:creator>
      <dc:date>2025-04-02T19:09:13Z</dc:date>
    </item>
  </channel>
</rss>

