简体   繁体   English

React-router-dom v6,URL 更改但组件不呈现

[英]React-router-dom v6, URL changing but component doesn't render

I've tried everything but fail to render component when URL changes.当 URL 更改时,我已经尝试了所有方法,但无法渲染组件。 No error messages nothing, it renders Home component but when i click on (view and edit icon)Link it just changes url, component does not render nothing happens.没有错误消息什么都没有,它呈现主页组件但是当我点击(查看和编辑图标)链接它只是改变 url,组件不呈现什么都没有发生。 I couldn't find a solution, is there any way to make it work?我找不到解决方案,有什么办法让它工作吗?

App.js应用程序.js

import "./App.css";
// import { TextFilledComp } from './Components/TextFilledComp';
import { Routes, Route } from "react-router-dom";
import { SingleTodoPage } from "./Components/SingleTodoPage";
import { EditTodo } from "./Components/EditTodo";
import { Home } from "./Components/Home";

function App() {
  return (
    <div>
      <Routes>
        <div>
          <div className="header-text">Todo List</div>
          <div className="box">
            <Route exact path="/" element={<Home />} />
            <Route path="/todo/:todoId" element={<SingleTodoPage />} />
            <Route path="/edit/:TodoId" element={<EditTodo />} />
          </div>
        </div>
      </Routes>
    </div>
  );
}

export default App;

Todo.js Todo.js

import {
  Checkbox,
  List,
  ListItem,
  ListItemSecondaryAction,
  ListItemText,
  makeStyles
} from "@material-ui/core";
import DeleteIcon from "@material-ui/icons/Delete";
import EditIcon from "@material-ui/icons/Edit";
import CheckBoxIcon from "@material-ui/icons/CheckBox";
import React from "react";
import { useSelector } from "react-redux";
import { Link } from "react-router-dom";

const useStyles = makeStyles({
  listRoot: {
    borderWidth: "1px",
    borderColor: "#aaaaaa",
    borderStyle: "solid",
    borderRadius: "20px"
  }
});

export const TodoList = () => {
  const todos = useSelector((state) => state.todo);
  const classes = useStyles();
  return (
    <div style={{ width: "95%", margin: "10px auto" }}>
      <List>
        {todos.map((todo) => (
          <ListItem key={todo.id} className={classes.listRoot}>
            <ListItemText primary={todo.name} />
            <ListItemSecondaryAction>
              {/* <Checkbox
                edge="end"
              /> */}
              <CheckBoxIcon color="primary" />
              <DeleteIcon color="secondary" />
              <Link to={`/edit/${todo.id}`} className="button">
                <EditIcon />
              </Link>
              <Link to={`/todo/${todo.id}`}>view</Link>
            </ListItemSecondaryAction>
          </ListItem>
        ))}
      </List>
    </div>
  );
};

codesandbox link for complete app完整应用程序的代码框链接

A Routes component is the replacement for Switch from v5 and should only wrap around Route components. Routes组件是从 v5 开始的Switch的替代品,并且应该只包裹Route组件。 It's solely responsible for matching the provided paths and controlling the visibility of Routes and does not know what to do with regular JSX.它只负责匹配提供的路径并控制路由的可见性,并且不知道如何处理常规 JSX。

function App() {
  return (
    <div>
      <div>
        <div className="header-text">Todo List</div>
        <div className="box">
          <Routes>
            <Route path="/" element={<Home />} />
            <Route path="/todo/:todoId" element={<SingleTodoPage />} />
            <Route path="/edit/:todoId" element={<EditTodo />} />
          </Routes>
        </div>
      </div>
    </div>
  );
}

I've also removed the exact prop as it is deprecated in v6 since all Routes are exact by default.我还删除了exact道具,因为它在 v6 中已被弃用,因为默认情况下所有路由都是准确的。 To allow non-exact Routes, use the new path="/nonexact/*" syntax.要允许非精确路由,请使用新的path="/nonexact/*"语法。 Some more info on the new features can be found here .可以在此处找到有关新功能的更多信息。

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

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