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

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

Params from Route

问题

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

<Switch>
    <Route
        render={({ location }) => {
            const { pathname } = location;
            const pathvalue = { pathname }

            return (
                <>
                    <div>
                    </div>

                    <div className="container">
                        {
                            data.pathvalue.map((rows, i) => { //而不是告诉页面名称,我将页面名称存储在pathvalue const中。这是否可能?
                                return (
                                    <div key={i} className="row">
                                        {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.

        &lt;Switch&gt;
            &lt;Route
            render={({ location }) =&gt; {
            const {pathname} = location;
            const pathvalue = {pathname}

            return (
            &lt;&gt;	
            &lt;div&gt;
            &lt;/div&gt;


			&lt;div className=&quot;container&quot;&gt;
            {                	
				data.pathvalue.map((rows, i) =&gt; { //Instead of telling the page name, I stored the page name in pathvalue const. Is it possible?
					return (
						&lt;div key={i} className=&quot;row&quot;&gt;							
							{rows.row.map((component, i) =&gt; { 

答案1

得分: 0

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

export default function Nav() {
  return (
    &lt;Router&gt;
      &lt;Switch&gt;
        &lt;Route
          render={({ location }) =&gt; {
            const { pathname } = location;
            return &lt;div&gt;{pathname}&lt;/div&gt;;
          }}
        /&gt;
      &lt;/Switch&gt;
    &lt;/Router&gt;
  );
}

然后,您可以在 &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

export default function Nav() {
  return (
    &lt;Router&gt;
      &lt;Switch&gt;
        &lt;Route
          render={({ location }) =&gt; {
            const { pathname } = location;
            return &lt;div&gt;{pathname}&lt;/div&gt;;
          }}
        /&gt;
      &lt;/Switch&gt;
    &lt;/Router&gt;
  );
}

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:

确定