I have a simple React component injected to the DOM as content script in my Chrome Extension, running on a website which is not mine.
I want to update (re-render) the component on every navigation (url change) made by the user on the website which my React app is injected to.
I figured React Router should be the best tool for this job.
However, it doesn't seem to work.
I found some related questions: this, this, this but none of the solutions work for me.
I tried to add a unique key to the Route, (like using useLocation or simply using location.href or even a Math.random() for the test) - but none of those make the component update.
The weird thing is that if I attach an onclick that changes a state to the div in my component, it does successfully makes the component re-render and update.
My content script looks like this:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
const myDiv = document.createElement('div');
myDiv.id = 'my-id';
document.body.appendChild(myDiv);
ReactDOM.render(<App />, myDiv);
My Routes (react-router v6):
import {
BrowserRouter,
Routes,
Route,
useParams,
useLocation
} from "react-router-dom";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/user/:id/" element={<MyComponent />} />
</Routes>
</BrowserRouter>
);
}
My component:
function MyComponent(props) {
const { id } = useParams();
React.useEffect(() => {
// Some stuff
}, [id])
return <div> User {id}</div>
My manifest.json scripts look like:
"background": {
"scripts": [
"build/background.js"
]
},
"content_scripts": [
{
"matches": [
"https://example-website.com/*"
],
"js": [
"build/content.js"
],
"css": [
"build/css/content.css"
]
},
{
"matches": [
"https://example-website.com/*"
],
"js": [
"build/reactApp.js"
],
"css": [
"build/css/content.css"
]
}
],