TypeScript error after upgrading version 4 useParams () from react-router-dom Property 'sumParams' does not exist on type '{}'

前端 未结 3 988
刺人心
刺人心 2021-02-05 02:14

I get a typeScript error after upgrading to version 4 Used in useParams () from react-router-dom

"typescript": "^4.0.2"

         


        
相关标签:
3条回答
  • 2021-02-05 02:26

    useParams is generic. You need to tell typescript which params you are using by specifying the value of the generic

    There are several ways to solve this

    This is my favorite way

    const { sumParams } = useParams<{ sumParams: string }>();
    

    But there are a few more ways (:

    OR

    interface ParamTypes {
      sumParams: string;
    }
    

    Then in your Component

    const { sumParams } = useParams<ParamTypes>();
    

    OR

    add any type without interface

    const { sumParams } : any = useParams();
    

    Note: that this way you will not be able to set it as a string

    OR

    More option for keemor:

    const { sumParams } = useParams() as { 
      sumParams: string;
    }
    
    0 讨论(0)
  • 2021-02-05 02:30

    Another option is:

    const { sumParams } = useParams() as { 
      sumParams: string;
    }
    
    0 讨论(0)
  • 2021-02-05 02:35

    To make it function as before, just add ":any"

    const { sumParams } : any = useParams();
    
    0 讨论(0)
提交回复
热议问题