“Params from Route” 可以翻译为 “路由参数”。

huangapple go评论184阅读模式
英文:

Params from Route

问题

我想要从JSON中映射pathvalue。 pathvalue将包含需要从JSON加载的页面名称。

  1. <Switch>
  2. <Route
  3. render={({ location }) => {
  4. const { pathname } = location;
  5. const pathvalue = { pathname }
  6. return (
  7. <>
  8. <div>
  9. </div>
  10. <div className="container">
  11. {
  12. data.pathvalue.map((rows, i) => { //而不是告诉页面名称,我将页面名称存储在pathvalue const中。这是否可能?
  13. return (
  14. <div key={i} className="row">
  15. {rows.row.map((component, i) => {

请注意,代码中的注释(//而不是告诉页面名称,我将页面名称存储在pathvalue const中。这是否可能?)已经保留在翻译中。

英文:

I want the pathvalue to be mapped from JSON. The pathvalue will have the page name which needs to load from JSON.

  1. &lt;Switch&gt;
  2. &lt;Route
  3. render={({ location }) =&gt; {
  4. const {pathname} = location;
  5. const pathvalue = {pathname}
  6. return (
  7. &lt;&gt;
  8. &lt;div&gt;
  9. &lt;/div&gt;
  10. &lt;div className=&quot;container&quot;&gt;
  11. {
  12. data.pathvalue.map((rows, i) =&gt; { //Instead of telling the page name, I stored the page name in pathvalue const. Is it possible?
  13. return (
  14. &lt;div key={i} className=&quot;row&quot;&gt;
  15. {rows.row.map((component, i) =&gt; {

答案1

得分: 0

&lt;Router /&gt; 组件只接受一个子组件。您可以在这里查看文档

  1. export default function Nav() {
  2. return (
  3. &lt;Router&gt;
  4. &lt;Switch&gt;
  5. &lt;Route
  6. render={({ location }) =&gt; {
  7. const { pathname } = location;
  8. return &lt;div&gt;{pathname}&lt;/div&gt;;
  9. }}
  10. /&gt;
  11. &lt;/Switch&gt;
  12. &lt;/Router&gt;
  13. );
  14. }

然后,您可以在 &lt;Router /&gt; 组件之外包含 &lt;nav /&gt; 组件,或者在内部包含一个包含两者的单个 &lt;div /&gt;

英文:

The &lt;Router /&gt; component only takes a single child component. You can see it in the documentation here

  1. export default function Nav() {
  2. return (
  3. &lt;Router&gt;
  4. &lt;Switch&gt;
  5. &lt;Route
  6. render={({ location }) =&gt; {
  7. const { pathname } = location;
  8. return &lt;div&gt;{pathname}&lt;/div&gt;;
  9. }}
  10. /&gt;
  11. &lt;/Switch&gt;
  12. &lt;/Router&gt;
  13. );
  14. }

You can then either include the &lt;nav /&gt; component outside the &lt;Router /&gt; component, or include a single &lt;div /&gt; inside with both included.

huangapple
  • 本文由 发表于 2020年1月7日 00:56:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定