【问题标题】:React router renders blank page using exact on all routesReact路由器在所有路由上使用精确呈现空白页面
【发布时间】:2021-05-22 23:46:40
【问题描述】:

我的一个组件有问题,其中 react 路由器在访问它时返回一个空白页面。它只发生在一个组件中,我不知道为什么。有问题的组件是 EditPost 组件。

App.js 这是所有路线的所在。

import "./App.css";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import Home from "./components/Home.js";
import Signup from "./components/signup/Signup";
import Login from "./components/Login/Login";
import Dashboard from "./components/Dashboard/Dashboard";
import PrivateRoute from "./components/common/PrivateRoutes";
import { Provider } from "react-redux";
import Store from "./Store";
import { Provider as AlertProvider } from "react-alert";
import AlertTemplate from "react-alert-template-basic";
import { loadUser } from "./actions/auth";
import { useEffect } from "react";
import PostPage from "./components/PostPage/PostPage";
import EditPost from "./components/EditPost/EditPost";

// Alert options
const alertOptions = {
  timeout: 3000,
};

function App() {
  useEffect(() => {
    Store.dispatch(loadUser());
  }, []);

  return (
    <div className="App">
      <Provider store={Store}>
        <AlertProvider template={AlertTemplate} {...alertOptions}>
          <Router>
            <Switch>
              <Route exact path="/post-edit/:id" component={EditPost} />
              <Route exact path="/signup" component={Signup} />
              <Route exact path="/login" component={Login} />
              <PrivateRoute exact path="/dashboard" component={Dashboard} />
              <Route exact path="/" component={Home} />
              <Route exact path="/post/:id" component={PostPage} />
            </Switch>
          </Router>
        </AlertProvider>
      </Provider>
    </div>
  );
}

export default App;

EditPost.js 这是不渲染的组件

import React, { useState } from "react";
import { Form, Button } from "react-bootstrap";
import { connect } from "react-redux";
import { updatePost } from "../../actions/posts";

const EditPost = (props) => {
  console.log(props);
  const [title, setTitle] = useState(props.location.postData.title);
  const [description, setDescription] = useState(
    props.location.postData.description
  );
  const [body, setBody] = useState(props.location.postData.body);

  const titleChange = (e) => {
    setTitle(e.target.value);
  };

  const bodyChange = (e) => {
    setBody(e.target.value);
  };

  const desChange = (e) => {
    setDescription(e.target.value);
  };

  const addClick = (e) => {
    e.preventDefault();
    const post = { title, body, description };
    props.updatePost(post);
  };

  return (
    <div className="mt-4">
      <h1>Edit Post</h1>
      <Form>
        <Form.Group>
          <Form.Label>Post Title</Form.Label>
          <Form.Control onChange={titleChange} type="text" />
        </Form.Group>
        <Form.Group>
          <Form.Label>Short Description</Form.Label>
          <Form.Control onChange={desChange} type="text" />
        </Form.Group>
        <Form.Group>
          <Form.Label>Body</Form.Label>
          <Form.Control onChange={bodyChange} as="textarea" rows={3} />
        </Form.Group>
        <Button onClick={addClick} variant="primary" type="submit">
          Edit
        </Button>
      </Form>
    </div>
  );
};

const mapDispatchToProps = (dispatch) => {
  return {
    updatePost: (id, post) => dispatch(updatePost(id, post)),
  };
};

export default connect(null, mapDispatchToProps)(EditPost);

Post.js 在这个组件中是 EditPost 组件的链接。

import React from "react";
import { Card } from "react-bootstrap";
import dateFormat from "dateformat";
import { Link } from "react-router-dom";
import { connect } from "react-redux";

const Post = (props) => {
  return (
    <Card>
      <Card.Body>
        <Link to={`/post/${props.id}`}>
          <Card.Title>{props.title}</Card.Title>
        </Link>
        <Card.Text>{props.description}</Card.Text>
      </Card.Body>
      <Card.Footer>
        <div className="d-flex justify-content-between">
          <small className="text-muted">
            {dateFormat(props.created, "mmmm dS, yyyy")}
          </small>
          {props.postUser === props.user ? (
            <Link to={`/edit-post/${props.id}`}>
              <i className="fas fa-edit" style={{ color: "grey" }}></i>
            </Link>
          ) : null}
        </div>
      </Card.Footer>
    </Card>
  );
};

const mapStateToProps = (state) => {
  if (state.auth.isAuthenticated) {
    return {
      user: state.auth.user.username,
    };
  } else {
    return {};
  }
};

export default connect(mapStateToProps)(Post);

现在我只是想渲染组件,然后我会担心道具。提前致谢。

【问题讨论】:

    标签: javascript reactjs redux react-router react-router-dom


    【解决方案1】:

    您的链接是/edit-post/${props.id}。 您匹配的路径是/post-edit/:id

    edit-postpost-edit 不是一回事?

    我建议在 Switch 的末尾添加一个 NoMatch 块。它将帮助您更快地诊断这些问题。

    <Route>
      <NoMatch />
    </Route>
    

    【讨论】:

    • 天哪,太感谢你了,我被这个弄坏了。
    • 不客气@Diegoa87,请不要忘记将此标记为accepted-answer ?
    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多