question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

router not defined with custom express server

See original GitHub issue

Ever since I added custom Express server to handle clean URLs, withRouter HOC doesn’t inject props.router.

Navigating FROM a page where withRouter is not used, TO a page withRouter, router prop is present, however, if the page is refreshed, the router prop is gone.

This is my current server.js setup for the clean urls.:

   server.get('/c/learn/:courseSlug/:lessonSlug', (req, res) => {
      const actualPage = '/course/learn';
      const queryParams = {
        courseSlug: req.params.courseSlug,
        lessonSlug: req.params.lessonSlug
      };
      app.render(req, res, actualPage, queryParams);
    });

How to user withRouter and Router methods with clean URLs?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lfadescommented, Jun 14, 2019

@ScottAgirs You need to change the import from

import { SingleCourseLearn } from '../../components/SingleCourseLearn';

to

import SingleCourseLearn from '../../components/SingleCourseLearn';

The default export is the one with withRouter(). btw if you’re using Now then you don’t need the custom server you currently have, instead you can have a serverless build and add custom routes to now.json like here.

0reactions
ScottAgirscommented, Jun 14, 2019

https://bitbucket.org/ScottAgirs/ijstobe/src/master/

https://bitbucket.org/ScottAgirs/ijstofe/src/master/

  • sorry, if a bit messy.

The Component at stake is ijstofe/src/components/SingleCourseLearn.js just noticed that ijstofe/src/components/SingleCourse.js works fine with Router and/or withRouter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why this route is working in express when not defined ...
it means express will use or any request that comes with '/' - will be redirected to route defined on route/index page router.use("/user" ......
Read more >
router not defined with custom express server · Issue #7582
Ever since I added custom Express server to handle clean URLs, withRouter HOC doesn't inject props.router . Navigating FROM a page where ...
Read more >
Express/Node introduction - Learn web development | MDN
This object, which is traditionally named app , has methods for routing HTTP requests, configuring middleware, rendering HTML views, registering ...
Read more >
Writing middleware for use in Express apps
The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.
Read more >
Advanced Features: Custom Server - Next.js
Before deciding to use a custom server, please keep in mind that it should only be used when the integrated router of Next.js...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found