1

Am trying to configure two routes in my application ... one for product page and the other for search results, I tried to do as follow

 <Router>
          <Switch>
            <Route exact path="/" component={Home} />
            <Template>
              <Route exact path="/search" component={SearchResult} />
              <Route path="/:id" component={PropertyTemplate} />
            </Template>
          </Switch>
        </Router>

Template

class Template extends React.Component {
      constructor(props) {
        super(props)
        this.state = {
          property: null
        }

      }

      render() {

        return (
          <div>
            {/* HEADER */}
            <Header></Header>
            {/* HEADER */}
            {/* LEFT SIDE */}
            <LeftSide></LeftSide>
            {/* LEFT SIDE */}
            <div id="wrapper">
              {this.props.children}
            </div>

          </div >
        )

      }
    }


    export default Template;

but it's not working, both components of /search and /:id are executed

note : id is alphanumeric

8
  • 1
    By "/search is matched also in /:id" what do you mean? Are both components rendered? Commented Feb 8, 2020 at 22:19
  • 1
    What is the Template component? Commented Feb 8, 2020 at 22:24
  • 1
    You might want to move your Switch component (or have another one) wrapping your two innermost Routes Commented Feb 8, 2020 at 22:32
  • 1
    @Chris, adding anohter switch did it ... Commented Feb 8, 2020 at 22:37
  • 1
    Great! I'll add that as an answer. Commented Feb 8, 2020 at 22:38

1 Answer 1

2

You might want to move your Switch component (or have another one) wrapping your two innermost Routes.

<Router>
  <Switch>
    <Route exact path="/" component={Home} />
      <Template>
        <Switch>
          <Route exact path="/search" component={SearchResult} />
          <Route path="/:id" component={PropertyTemplate} />
        </Switch>
      </Template>
    </Switch>
  </Router>
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.