1

I want to do this kind of routing: localhost:3000/function/page where function tells the app which function should take care of this url, and page tells which part we are inside a specific function.

const App: React.FC = () => {
  return (
    <Router>
      <div className="App">
        <Route component={SignUp} path="/signup/:page" />
      </div>
    </Router>
  );
}

interface MatchProps extends RouteComponentProps<any> {
  page: string;
}

class SignUp extends React.Component<MatchProps> {
  public render(){
    console.log(this.props.page)
  }
}

Then I access this page via localhost:3000/signup/1, in console I can only get an undefined result.

2 Answers 2

1

This is the solution:

import { match } from "react-router-dom";

interface DetailParams {
  id: string;
}

interface DetailsProps {
  required: string;
  match?: match<DetailParams>;
}


class Signup extends React.Component<DetailsProps, any> {
  render(){
    const match = this.props.match;
    console.log(match.params.page)
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use this.props.match.params.page instead of this.props.page solves the problem.

But it's still not perfect. this.props.match.params.page has a type as any. How can I restrict it.

2 Comments

I recommend asking follow-up questions by editing your post, or creating a new question.
@ggorlen sure. Thanks. I’m searching for existing answers first.

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.