<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rupeshsnss</title>
    <description>The latest articles on DEV Community by rupeshsnss (@rupeshsnss).</description>
    <link>https://dev.to/rupeshsnss</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F984977%2Fbb1e3ae5-5f18-4c4b-820b-135c7e3eb511.png</url>
      <title>DEV Community: rupeshsnss</title>
      <link>https://dev.to/rupeshsnss</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rupeshsnss"/>
    <language>en</language>
    <item>
      <title>Getting Invalid Hook Call Warning when trying to integrate React with exiting web application</title>
      <dc:creator>rupeshsnss</dc:creator>
      <pubDate>Tue, 06 Dec 2022 05:33:51 +0000</pubDate>
      <link>https://dev.to/rupeshsnss/getting-invalid-hook-call-warning-when-trying-to-integrate-react-with-exiting-web-application-38j4</link>
      <guid>https://dev.to/rupeshsnss/getting-invalid-hook-call-warning-when-trying-to-integrate-react-with-exiting-web-application-38j4</guid>
      <description>&lt;p&gt;We have a web application that is built using JSP pages. We are trying to migrate UI to React. Migration needs to be incremental as it's a huge application and we cannot migrate it completely in one go.&lt;/p&gt;

&lt;p&gt;We are trying to run a poc to see how we will integrate react components in phased manner. We are able to integrate a vanilla react component (a static Select) following this &lt;a href="https://beta.reactjs.org/learn/add-react-to-a-website"&gt;React Docs page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Problem comes when we started using useState hook. We started to get "Invalid Hook Call Warning".&lt;/p&gt;

&lt;p&gt;We created a react app and created components there, it works as react application. We converted JSX components to plain JS using Babel cli (steps as mentioned on the React Doc Page).&lt;/p&gt;

&lt;p&gt;Next we loaded React and React-DOM in the application through script tag as suggested on the page, except that we downloaded the script and referred from the file system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="https://unpkg.com/react@18/umd/react.production.min.js" crossorigin&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js" crossorigin&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type="text/javascript" src="&amp;lt;path to component JS&amp;gt;"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we tried to load the Select component in the target DIV element, we got the hook warning.&lt;/p&gt;

&lt;p&gt;I extracted code into a sample html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
    &amp;lt;head&amp;gt;
        &amp;lt;title&amp;gt;My Page&amp;lt;/title&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
        &amp;lt;h1&amp;gt;Try React&amp;lt;/h1&amp;gt;
        &amp;lt;div id="targetDiv"&amp;gt;
            &amp;lt;h5&amp;gt;Place content here&amp;lt;/h5&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;script type="text/javascript" src="./react/react.development.js"&amp;gt;&amp;lt;/script&amp;gt;
        &amp;lt;script type="text/javascript" src="./react/react-dom.development.js"&amp;gt;&amp;lt;/script&amp;gt;
        &amp;lt;script type="text/javascript" src="./react/components/core/coreSelect.js"&amp;gt;&amp;lt;/script&amp;gt;
        &amp;lt;script type="text/javascript"&amp;gt;
            function getSelectOptions() {
                const options = [];
                options.push({ text: "Select...", value: "" });
                options.push({ text: "Arizona", value: "AZ" });
                options.push({ text: "Canada", value: "CA" });
                options.push({ text: "Europe", value: "EU" });
                options.push({ text: "Hawai", value: "HW" });
                options.push({ text: "Mexico", value: "MX" });
                options.push({ text: "New York", value: "NY" });
                return options;
            };
            let selectArgs = {id:"mySelect", name: "mySelect", options: getSelectOptions(), value: "CA"};
            let root = document.getElementById('targetDiv');
            console.log({root});
            ReactDOM.createRoot(root).render(Select(selectArgs));
        &amp;lt;/script&amp;gt;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following is the content of coreSelect.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var _slicedToArray = function () { 
    function sliceIterator(arr, i) { 
        var _arr = []; 
        var _n = true; 
        var _d = false; 
        var _e = undefined;
        try { 
            for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { 
                _arr.push(_s.value); 
                if (i &amp;amp;&amp;amp; _arr.length === i) 
                    break; 
            } 
        } catch (err) { 
            _d = true; _e = err; 
        } finally { 
            try { 
                if (!_n &amp;amp;&amp;amp; _i["return"]) 
                    _i["return"](); 
            } finally { 
                if (_d) throw _e; 
            } 
        } 
        return _arr; 
    }
    return function (arr, i) { 
        if (Array.isArray(arr)) { return arr; } 
        else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } 
        else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } 
    }; 
}();


function Select(_ref4) {
    var id = _ref4.id,
        name = _ref4.name,
        value = _ref4.value,
        options = _ref4.options;

    var optArray = options ? options : [{ text: 'Select', value: '' }];

    console.log("Before useState7", {useState});
    var _useState7 = React.useState(options ? options : [{ text: 'Select', value: '' }]),
        _useState8 = _slicedToArray(_useState7, 2),
        optArray = _useState8[0],
        setOptArray = _useState8[1];

    console.log("Before useState9");
    var _useState9 = React.useState(value),
        _useState10 = _slicedToArray(_useState9, 2),
        selectedVal = _useState10[0],
        setSelectedVal = _useState10[1];

    console.log("Before useState11");
    var _useState11 = React.useState(""),
        _useState12 = _slicedToArray(_useState11, 2),
        effectiveClasses = _useState12[0],
        setEffectiveClasses = _useState12[1];

    var disabled = options &amp;amp;&amp;amp; options.length &amp;gt; 0 ? false : true;
    var onFocusClass = "active";

    function processOnClick() {
        if (!effectiveClasses || effectiveClasses.search(onFocusClass) &amp;lt; 0) {
            setEffectiveClasses(function (prevClasses) {
                var newClasses = (prevClasses ? prevClasses.trim() + " " : "") + onFocusClass;
                return newClasses;
            });
        } else {
            setEffectiveClasses(function (prevClasses) {
                var newClasses = prevClasses.replace(onFocusClass).trim();
                return newClasses;
            });
        }
    }

    return React.createElement(
        "select",
//        { id: id, name: name, className: "active", defaultValue: value, onClick: processOnClick, disabled: disabled },
        { id: id, name: name, className: effectiveClasses, defaultValue: selectedVal, onClick: processOnClick, disabled: disabled },
        optArray &amp;amp;&amp;amp; optArray.map(function (opt) {
            var optValue = opt.value;
            var optText = opt.text;
            return React.createElement(
                "option",
                { key: optValue, value: optValue },
                optText
            );
        })
    );
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have modified the JS file as generated from babel cli to not use imports/exports. I have verified on browser console that React, ReactDOM and Select component are available.&lt;/p&gt;

&lt;p&gt;As an experiment I tried to run the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.createRoot(document.getElementById('targetDiv')).render(Select({id:"mySelect", name: "mySelect", options: getSelectOptions(), value: "CA"}));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;from browser console and I still got the react hook error.&lt;/p&gt;

&lt;p&gt;I have been trying to search internet to find a solution but all available posts work with npm and try to resolve issues with react version mismatch, but I could not find any that would discuss problem with react integration with existing non-react applications.&lt;/p&gt;

&lt;p&gt;Any help in this regard would be greatly appreciated.&lt;/p&gt;

</description>
      <category>help</category>
      <category>react</category>
      <category>reacthooks</category>
    </item>
  </channel>
</rss>
