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
255 views
in Technique[技术] by (71.8m points)

reactjs - REACT.JS PATH EXACT NOT RENDERING

import {BrowserRouter , Route} from 'react-router-dom';
import React from 'react';
import HomeScreen from './screens/HomeScreen';
import ProductScreen from './screens/ProductScreen';
function App() {
  return (
   <BrowserRouter>
      <div className="grid-container">
        <header className="row">
        <div>
           <a className="brand" href="index.html">SanaMall.</a>
        </div>
        <div>
            <a className="link" href="/cart"> Cart</a>
            <a className="link" href="/signin"> Sign In</a>
        </div>
    </header>
    <main>
       <Route  path= "/" component={HomeScreen}/>
       <Route path="/product/:id" component={ProductScreen}/>
    </main>
    <footer className="row center">
        All rights reserved.
    </footer>
    </div>
    </BrowserRouter>
  );
}
export default App;

So my problem is that when i put exact path on the component HomeScreen, it doesnt render anything and then the ProductScreen only appears on the browser. Then if the HomeScreen shows in the brower, whenever i clicked on a product, it only refreshes the HomeScreen and it doesn't show the ProductScreen.

Hope you guys could help me. This have been my problem for almost 3 days.

question from:https://stackoverflow.com/questions/65870774/react-js-path-exact-not-rendering

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

1 Reply

0 votes
by (71.8m points)

If this code can't work let me know

import {BrowserRouter ,Switch, Route} from 'react-router-dom';
import React from 'react';
import HomeScreen from './screens/HomeScreen';
import ProductScreen from './screens/ProductScreen';
function App() {
  return (
   <BrowserRouter>
      <div className="grid-container">
        <header className="row">
        <div>
           <a className="brand" href="index.html">SanaMall.</a>
        </div>
        <div>
            <a className="link" href="/cart"> Cart</a>
            <a className="link" href="/signin"> Sign In</a>
        </div>
    </header>

       <Switch>    
        <Route exact path="/" >
          <HomeScreen />
        </Route>
        <Route path="/product/:id">
          <ProductScreen />
        </Route>
      </Switch>

    
    <footer className="row center">
        All rights reserved.
    </footer>
    </div>
    </BrowserRouter>
  );
}
export default App;

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

...