gpt4 book ai didi

javascript - React router 4查询参数示例不起作用

转载 作者:行者123 更新时间:2023-12-01 01:26:46 25 4
gpt4 key购买 nike

问题:尝试在我的本地环境上实现 react 路由器查询参数示例。下面的代码来自他们的网站。

import React from "react";
import { BrowserRouter as Router, Link } from "react-router-dom";

function ParamsExample({ location }) {
let params = new URLSearchParams(location.search);

return (
<Router>
<div>
<p>
React Router does not have any opinions about how your parse URL query
strings. Some applications use simple key=value query strings, but
others embed arrays and objects in the query string. So it's up to you
to parse the search string yourself.
</p>
<p>
In modern browsers that support{" "}
<a href="https://developer.mozilla.org/en-US/docs/Web/API/URL">
the URL API
</a>
, you can instantiate a <code>URLSearchParams</code> object from{" "}
<code>location.search</code> and use that.
</p>
<p>
In{" "}
<a href="https://caniuse.com/#feat=url">
browsers that do not support the URL API (read: IE)
</a>{" "}
you can use a 3rd party library such as{" "}
<a href="https://github.com/sindresorhus/query-string">query-string</a>.
</p>
<div>
<h2>Accounts</h2>
<ul>
<li>
<Link to={{ pathname: "/account", search: "?name=netflix" }}>
Netflix
</Link>
</li>
<li>
<Link to={{ pathname: "/account", search: "?name=zillow-group" }}>
Zillow Group
</Link>
</li>
<li>
<Link to={{ pathname: "/account", search: "?name=yahoo" }}>
Yahoo
</Link>
</li>
<li>
<Link to={{ pathname: "/account", search: "?name=modus-create" }}>
Modus Create
</Link>
</li>
</ul>

<Child name={params.get("name")} />
</div>
</div>
</Router>
);
}

function Child({ name }) {
return (
<div>
{name ? (
<h3>
The <code>name</code> in the query string is "{name}"
</h3>
) : (
<h3>There is no name in the query string</h3>
)}
</div>
);
}

export default ParamsExample;

渲染时,我收到以下错误消息
TypeError:无法读取未定义的属性“搜索”
这非常简单,但是当我执行location时.search 在我的 console 中,我得到以下输出 "?name=zillow-group"
话虽这么说 location.search 是一个有效的属性,但不确定为什么 React 没有看到它。

最佳答案

当您在控制台中记录位置时,您正在记录 window.location 对象。然而,在您的组件中,定义了一个优先的位置属性。它没有在 react-router example 中明确显示,但它们正在某处渲染 ParamsExample 组件并传入 location 属性。

您可以使用withRouter hoc访问react-router位置属性(除其他外)。或使用 Route component 。这是an example :

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter as Router, Link, Route } from "react-router-dom";

function ParamsExample() {
return (
<Router>
<div>
<p>
React Router does not have any opinions about how your parse URL query
strings. Some applications use simple key=value query strings, but
others embed arrays and objects in the query string. So it's up to you
to parse the search string yourself.
</p>
<p>
In modern browsers that support{" "}
<a href="https://developer.mozilla.org/en-US/docs/Web/API/URL">
the URL API
</a>
, you can instantiate a <code>URLSearchParams</code> object from{" "}
<code>location.search</code> and use that.
</p>
<p>
In{" "}
<a href="https://caniuse.com/#feat=url">
browsers that do not support the URL API (read: IE)
</a>{" "}
you can use a 3rd party library such as{" "}
<a href="https://github.com/sindresorhus/query-string">
query-string
</a>
.
</p>
<div>
<h2>Accounts</h2>
<ul>
<li>
<Link to={{ pathname: "/account", search: "?name=netflix" }}>
Netflix
</Link>
</li>
<li>
<Link to={{ pathname: "/account", search: "?name=zillow-group" }}>
Zillow Group
</Link>
</li>
<li>
<Link to={{ pathname: "/account", search: "?name=yahoo" }}>
Yahoo
</Link>
</li>
<li>
<Link to={{ pathname: "/account", search: "?name=modus-create" }}>
Modus Create
</Link>
</li>
</ul>

<Route
render={({ location }) => {
let params = new URLSearchParams(location.search);
return <Child name={params.get("name")} />;
}}
/>
</div>
</div>
</Router>
);
}

function Child({ name }) {
return (
<div>
{name ? (
<h3>
The <code>name</code> in the query string is "{name}"
</h3>
) : (
<h3>There is no name in the query string</h3>
)}
</div>
);
}

function App() {
return <ParamsExample />;
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

关于javascript - React router 4查询参数示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com