javascript React Router 中的 match.url 到底是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47762475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:34:59  来源:igfitidea点击:

What exactly is match.url in React Router?

javascriptreactjsreact-router

提问by JorahFriendzone

I see in the React Router docs that you pass into components the matchprop, and you can call match.url, but you never seem to actually pass in anything to the component. So what exactly is match and where are you getting it from?

我在 React Router 文档中看到您将matchprop传递给组件,您可以调用match.url,但您似乎从未真正将任何内容传递给组件。那么究竟什么是匹配,你从哪里得到它?

回答by Franklin

Hope this helps: https://reacttraining.com/react-router/web/api/match

希望这有帮助:https: //reacttraining.com/react-router/web/api/match

You get matchin the props, usually when your component is called by a Route you will get it, and you can pass it down to other components as regular props to extract or use the information.

你得到match了 props,通常当你的组件被 Route 调用时你会得到它,你可以将它作为常规 props 传递给其他组件来提取或使用信息。

locationis also very useful when interacting with react-router. I usually use match to do rendering and redirecting depending on the isExactflag, and location to have the actual URL that the browser has.

location在与 react-router 交互时也非常有用。我通常使用匹配来根据isExact标志和位置进行渲染和重定向,以获得浏览器的实际 URL。

回答by Meet Zaveri

See match() is pure JavaScript's String property. So if match is used in JSX, then react also made it's custom property matchso that it does not conflict with main match()and it is global object for React Router.

参见 match() 是纯 JavaScript 的 String 属性。因此,如果在 JSX 中使用 match,那么React也将其设为自定义属性match,使其不与 main 冲突,match()并且它是 React Router 的全局对象。

I have faced this issue and worked out knowing that don't need to worry that it should be passed in props or state. In fact it is custom property in JSX which does not conflict with actual String.prototype's property.

我遇到过这个问题并且知道不需要担心它应该在 props 或 state 中传递。实际上它是 JSX 中的自定义属性,与实际的 String.prototype 属性不冲突。

So without messing your head, just think it as of match()property that is from global Object specially used in React Router

因此,不要搞砸了,只需将其视为match()来自 React Router 中专门使用的全局对象的属性即可