1

I've torn this example down to barebones to make it as simple as possible.

I have a HomePage with a Link to PostPage

The Link in HomePage wraps a Post component that just says "post" (was originally a list)

CodeSandbox

I've looked up more than a few similar questions and somehow none of them seem to help.

Using react-router-dom: ^5.3.0

App.js

import './App.css';
import { Router, Switch, Route } from 'react-router-dom'
import history from './history'

import HomePage from './pages/HomePage'
import PostPage from './pages/PostPage'

function App() {
  return (
    <div className="App">
      <Router history={history}>
        <Switch>
          <Route path='/' exact component={HomePage} />
          <Route path='/post' component={PostPage} />
        </Switch>
      </Router>
    </div>
  );
}

export default App;

HomePage

import React from 'react'
import { Link } from 'react-router-dom'
import Post from '../components/Post'

class HomePage extends React.Component {
    render() {
        return (
            <Link to='/post'>
                <Post />
            </Link>
        )
    }
}

export default HomePage

Post

const Post = () => <div>Post component</div>
export default Post

PostPage

const PostPage = () => <div>Post Page</div>
export default PostPage

history

import { createBrowserHistory } from 'history'
export default createBrowserHistory()
  • Seems to be a bug somewhere other than your code.. I downgraded to React 17 from 18 and it began working as intended. It's possible that React Router v5 does not support React 18. – Brian Thompson Apr 25 '22 at 14:02
  • Wow, I've actually been going crazy. I just downgraded React and ReactDOM to 17 and it triggered a re-render. Although then it gave me some "legacy" error and this "Target container is not a DOM element." error which keeps coming up on CodeSandbox no matter what I do. – Nate Osterfeld Apr 25 '22 at 14:08
  • You'll need to change the root rendering code to match the 17 style. – Brian Thompson Apr 25 '22 at 14:09
  • Correct. Man, I hope someone else finds this useful because I was losing my mind. Kind of unfortunate that they don't work together. Maybe I'll just migrate everything to the new versions. Really appreciate the help, though! – Nate Osterfeld Apr 25 '22 at 14:14
  • Actually, I think that I can keep the latest versions, but continue using the older root render. Seems to work still. – Nate Osterfeld Apr 25 '22 at 14:17
  • I'd probably recommend updating to RRD v6 when possible. The longer you hold out the harder it will be to update in the future (most times) – Brian Thompson Apr 25 '22 at 14:20
  • https://github.com/remix-run/react-router/issues/7870 – morganney Apr 25 '22 at 14:31
  • Yeah, as someone in the comments of that thread pointed out, having the history object is somewhat essential, but I'm sure there are workarounds. – Nate Osterfeld Apr 25 '22 at 14:47
  • React 18 and React Router Dom 5 can, and do, work together. https://stackoverflow.com/a/71833424/8690857 – Drew Reese Apr 25 '22 at 15:31

0 Answers0