React router dom:路由和路由器根本不工作

IT技术 javascript reactjs react-native react-router-dom
2021-05-05 18:03:51

我对做出react并尝试使用 react-router-dom 进行重定向还很陌生,我按照文档做了所有事情,但我的代码似乎不起作用。我收到关于未找到元素的错误,我不明白是什么导致了它。另外,我现在只是尝试使用地址栏而不是任何按钮或链接来访问该页面。

错误

Matched leaf route at location "/" does not have an element. This means it will render an <Outlet /> with a null value by default resulting in an "empty" page.

/register 的相同错误

索引.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { Route, BrowserRouter as Router, Routes } from 'react-router-dom';
import Register from "./components/Register";

ReactDOM.render( 
  <Router>
    <React.StrictMode>
      <Routes>
        <Route exact path="/register" component={Register} />
        <Route exact path="/" component={App} />
      </Routes>
      <App />
    </React.StrictMode>
  </Router>,
  document.getElementById('root')
);

reportWebVitals();

应用程序.js

import { Component } from 'react';

class App extends Component {
  render(){
    return (
      <div>
        HELLO WORLD
      </div>
    );
  }
}


export default App;

注册.js

import { useState } from "react";
import { useNavigate } from "react-router-dom";

export default function Register(){
    const history = useNavigate();
    const initialFormData = Object.freeze({
          email: '',
          password: '',
      });

      const [formData, updateFormData] = useState(initialFormData);

    const handleChange = (event) => {
      updateFormData({
              ...formData,
              // Trimming any whitespace
              [event.target.name]: event.target.value.trim(),
          });
    }

    const handleSubmit = (event) => {
      event.preventDefault();
      console.log(event.target.email.value);
      console.log(event.target.password.value);
    }

    return (
      <form onSubmit={handleSubmit}>
        <div>
          <label>
            Email: 
            <input type="text" value={formData.email} name="email" onChange={handleChange} />
          </label>
        </div>
        <div>
          <label>
            Password: 
            <input type="text" value={formData.password} name="password" onChange={handleChange}/>
          </label>
        </div>
        <input type="submit" value="Submit" />
      </form>
  );
}

2个回答

您没有正确渲染路由组件。它们应该在elementprop上呈现为 JSX,而不是对组件的引用这是版本 5 和 6 之间的重大更改react-router-dom另请注意,Route组件也不再需要exactprops,它们现在总是完全匹配。

ReactDOM.render( 
  <Router>
    <React.StrictMode>
      <Routes>
        <Route path="/register" element={<Register />} />
        <Route path="/" element={<App />} />
      </Routes>
      <App />
    </React.StrictMode>
  </Router>,
  document.getElementById('root')
);

你能稍微修改一下吗,index.js然后再试一次:

react-router-dom@5

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import {
    Switch,
    Route,
    BrowserRouter as Router,
} from "react-router-dom";
import Register from "./components/Register";

ReactDOM.render( 
  <Router>
    <React.StrictMode>
      <Switch>
        <Route exact path="/register" component={Register} />
        <Route exact path="/" component={App} />
      </Switch>
      <App />
    </React.StrictMode>
  </Router>,
  document.getElementById('root')
);

reportWebVitals();

react-router-dom@6

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import {
    Route,
    Routes,
    BrowserRouter as Router,
} from "react-router-dom";
import Register from "./components/Register";

ReactDOM.render(
    <Router>
        <React.StrictMode>
            <Routes>
                <Route path="/register" element={<Register />} />
                <Route path="/" element={<App />} />
            </Routes>
            <App />
        </React.StrictMode>
    </Router>,
    document.getElementById('root')
);

reportWebVitals();

这应该有效。