Cannot Read Property Pathname of Undefined September 1, 2019
Posted by kevinbe71 in Development, Javascript, node, React, TypeScript, Web Development.Tags: react-router, redux
add a comment
A new project I recently started requires a universal app and an electron client. I had already got the base of the SSR part done so I moved on to the electron app. I had started the whole project off with a template but it didn’t have the electron part, so I had to add that myself.
It was actually quite tough to get everything together for the electron app as I wanted it to use TypeScript and that necessitated two webpack configs (one for the main script and the other for the renderer).
Anyway, once I had that working I ran into one final problem: “Uncaught TypeError: Cannot read property ‘pathname’ of undefined” in the react-router.js file. It occurred on a line that was referencing “this.state.location.pathname”
I didn’t find an answer by doing a lot of scouring of the web. As a consequence I started reading the docs: electron, redux, and react router’s docs to be specific. Eventually I saw something that caught my eye: “import { BrowserRouter as Router, Route } from ‘react-router-dom'” – BrowserRouter AS Router! Wow, that’s pretty easy to miss: in all of the source code I’d reviewed I’d seen “” but hadn’t looked closely enough at the import statement (or perhaps it was missing?) In any case, this resolve the error.