How to stop React/Electron from rewriting GET requests
P粉426780515
P粉426780515 2023-09-08 20:46:58
0
1
555

I'm using React and Electron to create a native app and I want to pass a file path from my main Electron process to one of my React components to render HTML in the file. I decided to do this using a URL, so I used the following route:

export default function App() {
  return (
      <Router>
        <Routes>
            <Route path="/3D-Graph/*" element={
              <UserContext.Provider value={"3D-Graph"}>
                <GraphApp />
              </UserContext.Provider>
            } />

            <Route path="/" element={
              <UserContext.Provider value={"2D-Graph"}>
                <GraphApp />
              </UserContext.Provider>
            } />
        </Routes>
      </Router>
  );
}

Then I use this in the GraphApp component to get the HTML:

const htmlFile = new URLSearchParams(useLocation().search).get('graph-path')

However, when I run this command, I get the following output: Rewrite GET /index.html/3D-Graph/?graph-path=3D-Graph to /index.html

I don't know if this is done by Electron when I use the loadURL function, or if it's done by React during its routing process. How do I stop it from doing this so I can route the application? Or am I approaching this the wrong way?

P粉426780515
P粉426780515

reply all(1)
P粉504080992

I have sent the file path from main in the past like this:

main:

win.webContents.send(”file-path-reply”, ”PATH TO YOUR FILE”)

or:

ipcMain.on("eventFromRenderer", (event) => {
   event.sender.send(”file-path-reply”, ”PATH TO YOUR FILE”)
 }

Rendering:

const MyComponent = () => {
const [htmlPath, setHtmlPath] = useState(””);

useEffect(() => {
 const reply = (evt, message) => {      
 setHtmlPath(message);
}

ipcRenderer.on(”file-path-reply”, reply);

}, []);
// use htmlPath…
}

Hope this helps! The capitalization is a bit weird, I wrote it using the autocorrect function on my phone :)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template