简体   繁体   English

组件未呈现,问题 react-router

[英]component is not render, problems with react-router

I'm having a problem with react-route.我在反应路线上遇到问题。 The NoteList component is not rendering. NoteList组件未呈现。

app.js应用程序.js

import { BrowserRouter as Router, Routes } from 'react-router-dom';
import logoNotes from './img/logoNotes.png';
import NoteList from './components/NoteList/NoteList';
import './App.css';

function App() {
  return (
    <Router>
      <div className='aplication-notes'>
        <div className='notes-logo-container'>
          <img src={logoNotes} className='notes-logo' alt='logo' />
        </div>
        <div className='note-list'>
          <h1> my notes</h1>
          <br></br>
          <Routes
            path='/src/components/NoteList/NoteList'
            element={<NoteList />}
          />
        </div>
      </div>
    </Router>
  );
}

export default App;

I can see the div and h1 that is in the app.js file but not the Notelist component.我可以看到app.js文件中的divh1 ,但看不到Notelist组件。

How can I solve this problem?我怎么解决这个问题?

The Routes component is only responsible for wrapping Route components and handles the path matching duties. Routes组件只负责包装Route组件并处理路径匹配职责。 It doesn't take path or element props.它不采用pathelement道具。 These belong to the Route component.这些属于Route组件。

Import the Route component and render the NoteList component by the Route and wrap the Route with Routes .导入Route组件并通过Route渲染NoteList组件,并用Routes包裹Route

Example:例子:

import {
  BrowserRouter as Router,
  Routes
  Route // <-- import
} from 'react-router-dom';
import logoNotes from './img/logoNotes.png';
import NoteList from './components/NoteList/NoteList';
import './App.css';

function App() {
  return (
    <Router>
      <div className='aplication-notes'>
        <div className='notes-logo-container'>
          <img src={logoNotes} className='notes-logo' alt='logo' />
        </div>
        <div className='note-list'>
          <h1> my notes</h1>
          <br />
          <Routes> // <-- wrap Route components
            <Route // <-- Route renders content
              path="/src/components/NoteList/NoteList"
              element={<NoteList />}
            />
          </Routes>
        </div>
      </div>
    </Router>
  );
}

export default App;

Now when you navigate to "/src/components/NoteList/NoteList" in the browser you should see the UI rendered as well as the NoteList component.现在,当您在浏览器中导航到"/src/components/NoteList/NoteList"时,您应该会看到呈现的 UI 以及NoteList组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM