【问题标题】:Passing props inside Link React Typescript Route在 Link React Typescript Route 中传递道具
【发布时间】:2021-09-08 21:22:34
【问题描述】:

我需要将 Link react Router 内的 props 传递给另一个组件,但由于我不理解的接口或类型声明,我不能。我一直在四处寻找,但找不到任何与此问题有关的内容(Typescript)。

这是我的组件的代码:

const ImageGallery = () => {

   interface passProps {
      backUrl?: string
   }

   const backUrl = '/some/other/value';

   return (
      <Gallery>
         {images.map((x, i) => (
         <Link key={x} to={{ pathname: 'page', query: { backUrl } }}>
         ...

这是错误:

(property) backUrl: string
Type '{ pathname: string; query: { backUrl: string; }; }' is not assignable to type 'LocationDescriptor<unknown> | ((location: Location<unknown>) => LocationDescriptor<unknown>)'.
  Object literal may only specify known properties, and 'query' does not exist in type 'LocationDescriptorObject<unknown> | ((location: Location<unknown>) => LocationDescriptor<unknown>)'.ts(2322

【问题讨论】:

    标签: reactjs typescript react-router


    【解决方案1】:

    您可以通过state 属性向&lt;Link&gt; 组件添加状态。

    Previously answered here

    【讨论】:

      猜你喜欢
      • 2015-07-18
      • 2020-12-07
      • 2021-04-01
      • 2018-04-27
      • 2021-07-31
      • 2021-07-21
      • 1970-01-01
      • 2019-12-01
      • 2018-06-09
      相关资源
      最近更新 更多