0

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?

2
  • You need an api through e.g. express and call the endpoints from your react app Commented 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. Commented Jul 4, 2021 at 4:22

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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.