简体   繁体   English

React Router Link 更改路径,但不呈现新组件

[英]React Router Link changes path, but doesn't render new component

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.我正在尝试使用 React Router 4.2.2 来加载一个名为 PostFocus 的组件,每当我点击“卡片”组件时,它周围都有一个链接。 When I click on a 'Card' the path changes correctly, but the PostFocus component isn't rendered.当我单击“卡片”时,路径会正确更改,但不会呈现 PostFocus 组件。 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: index.js 代码:

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'));

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 :你的 App.js 或 index.js 中可能有 BrowseRouter,我的 index.js 中有它,如下所示:

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注意:-不要忘记在 index.js 中导入 Route

@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.. @Tom Karnaski 嗨...抱歉回复晚了,我没有时间处理它..你的代码正在我的系统中运行,我没有权限在你的仓库中推送一个分支..制作你的应用程序.js如下所示.. 它肯定会工作..

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;

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.不确定它是否正确,但它对我有用,希望它可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM