Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

javascript - Ionic React route changes url, page is blank

I am workin with Ionic 5 and React. I have a component called ShowListing that i am trying to display after being clicked upon. It is as follows

export const ShowListing = () => {
    const id = useParams()
    const listings = ShowListingEffect(id)
    return (
        
        <IonPage>
            {listings.map((list: any) => (
                <IonCard key={list.email}>
                <IonCardTitle >{list.title}</IonCardTitle>
                    <IonItem>{list.description}</IonItem>
                    <IonItem>{list.address}</IonItem>
                    <IonItem>{list.phone}</IonItem>
                    <IonItem>{list.pincode}</IonItem>
                    {/* <img src={'http://localhost:5000/' + list.path} /> */}
                </IonCard>
                
            ))}
            </IonPage>
        
    )
}

The route has been defined in App component, as follows

<IonApp>
    <Router>
        <Switch>
          <Route path='/LandingPage' component={LandingPage} />
          <Route path="/listings/:id" component={ShowListing} exact/>
          <Route path="/" render={() => <Redirect to="/LandingPage" />} exact={true} />
        </Switch>
      </Router>
  </IonApp>

The problem is, when i click on the link, a blank page is dispayed. If i refresh the page, i get the component. I have tried a number of things

a) Using BrowserRouter instead of IonReactRouter b) Using Switch inside IonRouterOutlet c) Without IonReactRouter and IonRouterOutlet

But to no avail. I should mention that without Switch, nothing gets displayed, even after refresh. Any ideas how to solve this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There was another page that was routed to from App.tsx, which contained routing mechanism as well. I put the line <Route path="/listings/:id" component={ShowListing} exact/> in that page and it worked, in addition to putting switch.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...