Add a script in <head> on only one specific page (React)

Viewed 885

I'm a beginner working with an existing React/Node/Express site. On one specific page I would like add a script in the head of that page. How can I do this?

I understand that the node module React-helmet can achieve this. But since it's something I only need once, I rather do this without an extra dependency. Is there some way to do this? For example, in the file where you manage routes or perhaps in index.html where the head is set?

1 Answers

You could look into dynamic imports in React

Basically, you can use import() to add the dependency in the page / rendered component that you are using and it will be loaded dynamically when you are on the page.

When Webpack comes across the import().then syntax, it automatically starts code-splitting your app. If you’re using Create React App, this is already configured for you and you can start using it immediately.

Related