使用REACT获取路径网址

时间:2018-04-23 14:44:31

标签: javascript reactjs react-component

当我正在使用反应时,我尝试从我的页面获取当前路径。我的想法是为每个页面创建规则,因为我的应用程序需要这个。

在第1个示例中,我创建了一个内联组件并且工作正常!

ecs-agent

我有正确的路径! (示例: / test / john)

但是,我不是这样使用的,我无法理解当我在真实组件中时如何重现 {match} ,如下所示:

const names = A.map((obj) => {
  if (obj.user) {
    return obj.user.firstname;
  }
});

我想,仍在阅读当前路径,在获取此信息后,我将使用我的代码创建一些条件。

例如: localhost:3000 / test / username - 路径应为: test / username

1 个答案:

答案 0 :(得分:0)

match道具只传递给<Route />中直接指定的组件,因此TestLink的子组件必须明确地赋予match道具。

<Route path="/test/:username" component={TestLink} />

const TestLink = ({match}) => {
    console.log(match.url);
    // pass the `match` prop to `CustomComponent`
    return <CustomComponent match={match} />;
}