如何读取条件类名称之类的类似react-router的路径?

时间:2019-07-15 20:34:10

标签: reactjs

我想根据路径更改页脚的className,就像React-Router在内部所做的那样。我需要(a)在parent(app)中创建一个状态,其中路由器来自交换机中的路径,然后将其传递,或者(b)读取child(footer)中的路径,并将其直接内联到className三元组中

谢谢

1 个答案:

答案 0 :(得分:0)

编辑:window.location.pathname起作用(我必须做错什么,因为它只有在刷新根目录后才给出url),用于条件className:必须对每个“ / page”检查都使用window.location.pathname。

className={
              window.location.pathname === "/chats"
                ? "chats_large2"
                : window.location.pathname === "/events"
                ? "chats_large2"
                : "chats_large"
            }

我也可以在相同的div或img中创建onclick = {this.handleClick}和

constructor(props) {
    super(props);
    this.state = { page: "" };
  }
  handleClick = () => {
    this.setState({ page: "/chats" });
  };
  handleClick2 = () => {
    this.setState({ page: "/planner" });
  };
上面的类组件中的

我还在脚注图标周围放置了<Route render={location => (<Link to=""><div></div></Link>)}/>,以使Router进入新状态和window.location.pathname时,可以重新显示页脚。location.pathname可以读取最新状态(有时,它也读取最新状态为路径,不一定是最新的路由器状态。

相关问题