【问题标题】:Post ID route showing up in other componentsPost ID 路由显示在其他组件中
【发布时间】:2019-03-31 10:05:32
【问题描述】:

我正在学习路由器和redux。我正在尝试创建一个博客。现在帖子 ID 路径显示在关于和投资组合等其他组件中,但不在主页中,因为我已将其设置为精确路径。

https://github.com/chiranjeebhub/router-redux

Post.js:

import React, { Component } from "react";
import { connect } from "react-redux";

class Post extends Component {
  render() {
    const post = this.props.post ? (
      <div>
        <h4>{this.props.post.title}</h4>
        <p>{this.props.post.body}</p>
      </div>
    ) : (
      <div>
        <p>loading post ... </p>
      </div>
    );
    return <div>{post}</div>;
  }
}

const mapStateToProps = (state, ownProps) => {
  let id = ownProps.match.params.post_id;
  return {
    post: state.posts.find(post => post.id == id)
  };
};

export default connect(mapStateToProps)(Post);

我的路由器:

import React, { Component } from "react";
import { BrowserRouter, Route } from "react-router-dom";
import NavBar from "./Components/NavBar";
import Home from "./Components/Home";
import About from "./Components/About";
import Post from "./Components/post";
import Portfolio from "./Components/portfolio";

class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <div className="App">
          <NavBar />
          <Route exact path="/" component={Home} />
          <Route path="/about" component={About} />
          <Route path="/portfolio" component={Portfolio} />
          <Route path="/:post_id" component={Post} />
        </div>
      </BrowserRouter>
    );
  }
}

export default App;

关于.js

import React, { Component } from "react";

class About extends Component {
  render() {
    return (
      <div>
        <h1>About Me</h1>
      </div>
    );
  }
}

export default About;

减速器

const initState = {
  posts: [
    { id: "1", title: "title1", body: "body1" },
    { id: "2", title: "title2", body: "body2" },
    { id: "3", title: "title3", body: "body3" },
    { id: "4", title: "title4", body: "body4" }
  ]
};

const rootReducer = (state = initState, action) => {
  return state;
};

export default rootReducer;

现在,当我加载“关于”组件时,“正在加载帖子...”会从 post.js 显示

【问题讨论】:

  • 当你加载portfolio时,同样的"Loading Posts..."会出现?
  • 是的。它出现在投资组合中

标签: reactjs redux react-redux redux-router


【解决方案1】:

您遇到问题是因为路径

<Route path="/:post_id" component={Post} />

表明post_id 占位符处应有一个动态值。由于post_id 只是一个占位符,使用aboutportfolio1 等作为路由参数将满足条件。

// All of them are valid
/about
/portfolio
/1

所以Post 组件与其他组件一起出现。

因此,您应该用Switch 组件包装Route 组件。由于Switch 组件只呈现与路径匹配的第一个子组件,因此Post 组件在使用/about/portfolio 时不会显示。

import { BrowserRouter, Route, Switch } from 'react-router-dom';

 <BrowserRouter>
    <div className="App">
      <NavBar />
      <Switch>
        <Route exact path="/" component={Home} />
        <Route path="/about" component={About} />
        <Route path="/portfolio" component={Portfolio} />
        <Route path="/:post_id" component={Post} />
      </Switch>
    </div>
  </BrowserRouter>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 2021-07-28
    • 2020-04-09
    • 2020-09-16
    相关资源
    最近更新 更多