1

I got a route which may have different syntax according to the tokenid

<Switch>
        <Route exact path="/">
          <HomePageComponent />
        </Route>
        <Route path="/verify/:tokenId" component={EmailTarget}>
</Switch>

What i am trying to do is i am trying to grab what is in the place of :tokenId.

Here what code look like inside the Email Target Component.

const EmailTarget: React.FC = ( props ) => {
    const [ loginState, setLoginState ] = useState('');

    useEffect(() => {
              console.log(props.match.params.tokenId)
    }, [])

What I am doing now is simply console logging the tokenId. But the typescript is giving me error.

Here is the error log

C:/Users/kerry/Desktop/works/rslabs/src/components/EmailTarget.tsx
TypeScript error in C:/Users/kerry/Desktop/works/rslabs/src/components/EmailTarget.tsx(14,27):
Property 'match' does not exist on type '{ children?: ReactNode; }'.

The strange thing about this situation is that I can console.log the tokenId and actually giving me the result but the program is giving me an error.

Please don't give minus to this question. If you give a minus please mention the area I need to improve in the question first.

6
  • What is the output for console.log(props)? Also, did you check reactrouter.com/web/example/url-params ? I know their example doesn't use typescript, but it uses a useParams() hook instead. Commented Oct 13, 2020 at 14:27
  • Are you using the withRouter HoC on your component? That is withRouter(EmailTarget) ? If not Typescript won't recognize you are trying to use React Router props. Commented Oct 13, 2020 at 14:30
  • this props actually return the same props as in react Commented Oct 13, 2020 at 14:32
  • @DiegoPedro actually i am using it I am exporting withRouter(App) like this Commented Oct 13, 2020 at 14:35
  • 1
    @Kerry Then take a look at @merko answer as it is the correct one. Typescript cannot infer you are in a Route and therefore able to access RouteComponentProps Commented Oct 13, 2020 at 14:39

1 Answer 1

1

You need to extend component props with RouteComponentProps type, it includes everything you need.

Example:

const Component = ({
  history,
  match: {
    params: { id },
  },
}: YourProps & RouteComponentProps<{ id: string }>)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.