Nodejs has many .js files stored and they are exp1.js, exp2.js,exp3.js. Reactjs app called ABTest where a menu is displayed and the user selects which exp to run. After selecting the exp then user clicks START button to run the selected exp's in the ABTest app. How to run the exp which is .js file in the ABTest reactjs app?
-
You need an api through e.g. express and call the endpoints from your react appTracer69– Tracer692021-07-03 17:33:14 +00:00Commented Jul 3, 2021 at 17:33
-
I use API to have some services running but don't have an idea how it could help in this scenario, let me know how it could be done.DBlearner– DBlearner2021-07-04 04:22:04 +00:00Commented Jul 4, 2021 at 4:22
Add a comment
|
1 Answer
It is not related to React. To dynamically load some modules you can use Webpack as follows:
const onSelectMenu = useCallback(async (menuId) => {
const module = await import(`${menuId}.js`);
...
}, []);
...
<Menu onSelect={onSelectMenu}>
<Item value="exp1">First</Item>
<Item value="exp2">Second</Item>
<Item value="exp3">Third</Item
</Menu>
https://webpack.js.org/guides/code-splitting/#dynamic-imports
More react examples: https://reactrouter.com/web/guides/code-splitting
1 Comment
DBlearner
if I would have many such exps added in the nodejs anytime in future and those should be made available to run or in other words it should be made dynamic . Don't worry about menu , I can fix that