0

I'm trying to use React Router 4.2.2 in order to load a component called PostFocus whenever I click on a 'Card' component, with a Link wrapped around it. When I click on a 'Card' the path changes correctly, but the PostFocus component isn't rendered. Have I done something wrong or missed something out in the Route? I can't figure it out.

Here is the code:

class PostsList extends React.Component {
    render() {
        var createCards = this.props.posts.map((post, i) => {
            return (
                <div key={i}>
                    <Link to={`/${post.id}`}>
                        <Card title={post.title} subtitle={post.subtitle} date={post.date} id={post.id}/>
                    </Link>
                    <Route exact path={`/${post.id}`} render={(post) => (
                        <PostFocus content={post.content}/> 
                    )}/>
                </div>
            );
        });
        return (
            <div>
                {createCards}
            </div>
        );
    }

App Component:

import React from 'react';
import PostsList from '../containers/posts_list.js';
import {withRouter} from 'react-router';

class App extends React.Component {
    render() {
        return(
            <div>
                <PostsList />
            </div>
        );
    }
}

export default withRouter(App);

index.js code:

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import ReduxPromise from 'redux-promise';
import { BrowserRouter } from 'react-router-dom';

import App from './components/App.jsx';
import reducers from './reducers';

const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);

ReactDOM.render(
        <Provider store={createStoreWithMiddleware(reducers)}>
            <BrowserRouter>
                <App />
            </BrowserRouter>
        </Provider>
, document.getElementById('root'));
user74843
  • 611
  • 1
  • 8
  • 24

3 Answers3

4

I was also facing the same problem. I fixed it with a trick. You might have BrowseRouter in your App.js or index.js, I had it in my index.js like this :

ReactDOM.render(<BrowserRouter>
                    <App />
                </BrowserRouter>, document.getElementById('root'))

I changed it to :-

ReactDOM.render((
                    <BrowserRouter>
                      <Route component={App} />
                    </BrowserRouter>
                  ), document.getElementById('root'))

and it tricked, actually we are keeping the router look over our complete application by doing this, thus it also checks up with the routing path and automatically renders the view. I hope it helps.

Note:- Do not forget to import Route in your index.js

Aamir Iqubal
  • 119
  • 7
0

@Tom Karnaski Hi... Sorry for the late reply, I was not getting time to work on it.. Your code is running in my system, I didn't had access to push a branch in your repo.. make your App.js like below.. it will work for sure..

import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom'
import Navbar from './Components/Navbar/Navbar';

class App extends React.Component {
  render() {
    return (
      <Router>
        <div className="App">
          <Route component={Navbar}/>
        </div>
      </Router>
    );
  }
}

export default App;
Aamir Iqubal
  • 119
  • 7
-1

I solved this problem simply use tag instead of Link helper. Not sure is it right or not, but it works for me, hope it will helps anybody else.

Ruslan Valeev
  • 400
  • 4
  • 12