1

Consider having a web application that is based on a complex platform, however, you can design HTML Forms using its designer, and insert standard HTML elements, one of which is HTML Component. You can use this component to add HTLM/JavaScript code as usual.

I followed a tutorial to create a react app without using create-react-app. I managed to develop a sample React app using NodeJs, Babel, and Webpack and managed to deploy the final bundled JavaScript main.js on the target application. All worked fine. Below are the main two files with the source code:

index.js

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";

ReactDOM.render(<App />, document.querySelector("#root"));

App.js

import React, {useState} from "react";
const App = () => {
    let [counter, setCounter] = useState(0);
    const increment = () => {
        counter++;
        setCounter(counter);
    }

    return <div>This was created from React and Hello World!
        <h1>This is a test Header</h1>
        <button onClick={increment}>Click here to increment</button>
        <br/>
        <span>{counter}</span>
    </div>
}

export default App;

The Form HTML Component in the Target Web Application:

<div id="root">This Text should be replaced by the React App</div>
<script src="./target/app/path/public/main.js"></script>

Mind you that I ran the command npm run build and it generated the final script main.js which is deployed to the target application.

I am planning to start using React to build specific components for example a combination of Drop-Down Fields and Grid elements which are populated using REST APIs.

The problem is that I have to have one entry JavaScript source file that will render the component using ReactDOM.render(<App />, document.querySelector("#root")), and if I develop say 100 components, but not all of them will be rendered when any Application Form is loaded. I think if the React render() function is invoked and the target DOM element is not present, then it will throw an error (I am not sure though).

So the question is how to design the application so that I can follow the above approach and ensure that the intended React Component will kick in only when the related Form is loaded or active.

I am thinking to import all the Components in index.js and develop a method to detect if the Form is active and needs a component, then it will invoke the function ReactDOM.render(<MyComponent />, document.querySelector("#MyComponentId")).

I am not sure how to implement that and I need help. I appreciate your feedback.

enter image description here enter image description here

3
  • Hi. Did you find a solution to your approach? I have a similar issue and thinking the same way you are. Commented Apr 14 at 19:42
  • As they say, great minds think alike! It was too long ago, I'd love to go back to those moments... Commented Apr 15 at 23:08
  • No need, I figured it out. Here is the solution I came up with: stackoverflow.com/a/79569809/1795917 Commented Apr 16 at 13:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.