简体   繁体   English

如何为具有不同价值道具的不同路线呈现相同的反应成分?

[英]How to render same react component for different routes with different value props?

I want to ask whether it is possible to render same react component for different routes but with different value props: 我想问问是否可以为不同的路线渲染相同的反应组件,但具有不同的价值道具:

I have something like this: 我有这样的事情:

   <Switch>
      <Route
         exact
         path="/something1"
         render={props => (
            <SomeComponent
             {...props}
             buttonStyle="#65BDE0"
          />
       )}
    />
    <Route
       exact
       path="/something2"
       render={props => (
          <SomeComponent
           {...props}
           buttonStyle="#FFFFFF"
          />
       )}
    />
     <Route
       exact
       path="/something3"
       render={props => (
          <SomeComponent
           {...props}
           buttonStyle="#000000"
          />
       )}
    />
    </Switch>

As you can see I have three different routes with the same component but the buttonStyle is different for each route. 如您所见,我有三个具有相同组件的不同路线,但是每种路线的buttonStyle都不同。 Is there a way how to simplify this or some better apporach how to handle this ? 有没有一种方法可以简化此过程或更好的方法来处理此问题? For example with one Route component ? 例如带有一个Route组件? Thank you. 谢谢。

I doubt that you can do this with one <Route /> component since a Route can have only one path. 我怀疑您是否可以使用一个<Route />组件来执行此操作,因为Route只能具有一条路径。

However, you could make it a bit cleaner by creating an array of routes. 但是,您可以通过创建路由数组来使其更加整洁。

It could look something like this: 它可能看起来像这样:

const routesWithProps = [
  {
    path: '/something1',
    props: {
      buttonStyle: '#65BDE0'
    }
  },
  {
    path: '/something2',
    props: {
      buttonStyle: '#FFFFFF'
    }
  },
  {
    path: '/something3',
    props: {
      buttonStyle: '#000000'
    }
  },
];

<Switch>
  {
    routesWithProps.map(route => (
      <Route
         exact
         path={route.path}
         key={route.path}
         render={props => <SomeComponent {...props} {...route.props} />} 
      />
    ))
  }
</Switch>

That would depend on where you want to keep the logic. 那将取决于您想要保留逻辑的位置。 If you want to make the route simple you could have a single route component with something/:btn , and then in the <SomeComponent /> have the button rendering logic. 如果您想简化路线,则可以使用带有something/:btn的单个路线组件,然后在<SomeComponent />具有按钮呈现逻辑。 You could have the /:btn be the colored hash you want the button to be in, or some arbitary id number only your app could decipher. 您可以让/:btn是您希望按钮放入的彩色哈希,或者是某些只有您的应用才能解密的ID号。

An example would be 一个例子是

export const SomeComponent = ({history}) => {
  const param = history.params.btn;
  if(param === 1) {
    return <div>Here i can add specific things </div>
  }
  // and so on

}

This is not because it is the best solution, but maybe this idea will kickstart how to want to do it. 这不是因为它是最佳解决方案,而是这种想法可能会启动如何做的开始。 Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM