【问题标题】:Link props when passed does not work when browser back and forth is clicked React router v4单击浏览器来回时传递的链接道具不起作用React router v4
【发布时间】:2019-08-03 19:40:42
【问题描述】:

问题是...当您加载“/”主组件加载时,当您单击主题时,我正在从 Link 传递我的数据,这在 h1 标记中显示,所以工作正常....(已编辑) 问题是一旦您单击主题并单击后退按钮,该按钮将转到主页并再次单击前进。我可以看到从 Link 传递的数据不存在?

我希望数据即使在我返回并再次前进后也存在

在这里创建了一个小的工作用例https://codesandbox.io/s/m4lvm46myx

import React, { Component } from "react";
import ReactDOM from "react-dom";

import { BrowserRouter, Route, Link } from "react-router-dom";



const topics = [
  {
    name: "Functional Programming",
    id: "functional-programming",
    description:
      "In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.",
    resources: [
      {
        name: "Imperative vs Declarative programming",
        id: "imperative-declarative",
        description:
          "A guide to understanding the difference between Imperative and Declarative programming.",
        url: "https://tylermcginnis.com/imperative-vs-declarative-programming/"
      },
      {
        name:
          "Building User Interfaces with Pure Functions and Function Composition",
        id: "fn-composition",
        description:
          "A guide to building UI with pure functions and function composition in React",
        url:
          "https://tylermcginnis.com/building-user-interfaces-with-pure-functions-and-function-composition-in-react-js/"
      }
    ]
  }
];

const Resources = ({ match }) => {
  return <div>Resources</div>;
};

const Home = () => {
  return (
    <div>
      <h1>HOME </h1>
    </div>
  );
};

const Topic = ({ match }) => {
  console.log("match", match);
  const topic = topics.find(({ id }) => id === match.params.topicId);
  return (
    <div>
      <h1>{topic.name}</h1>

      <ul>
        {topic.resources.map(sub => (
          <li key={sub.id}>
            <Link to={`/topics/${match.params.topicId}/${sub.id}`}>
              {sub.name}
            </Link>
          </li>
        ))}
      </ul>
      <hr />
      <Route path={`/topics/:topicId/:subId`} component={Resources} />
    </div>
  );
};

const Topics = props => {
  console.log("Topics location props", props.location.name);
  return (
    <div>
      <h1>Link data passed : {props.location.name}</h1>
      <h1>Topics </h1>
      <ul>
        {topics.map(({ name, id }) => (
          <li key={id}>
            <Link to={`/topics/${id}`}> {name}</Link>
          </li>
        ))}
      </ul>
      <hr />
      <Route path={`/topics/:topicId`} component={Topic} />
    </div>
  );
};

class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <div className="App">
          <h1>Nested Routers Learnings </h1>
          <ul>
            <li>
              {" "}
              <Link to="/">Home</Link>
            </li>
            <li>
              {" "}
              <Link
                to={{
                  pathname: "/topics",
                  name: "Nischith"
                }}
              >
                Topics
              </Link>
            </li>
          </ul>

          <hr />

          <Route exact path="/" component={Home} />
          <Route path="/topics" component={Topics} />
        </div>
      </BrowserRouter>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));

【问题讨论】:

    标签: reactjs react-router-v4


    【解决方案1】:

    不是直接传递props,而是通过将其存储在父类组件的状态中来传递。

    import { createBrowserHistory } from 'history';
    
    const browserhistory = createBrowserHistory();
    
    • 将此对象嵌入到render()方法中的&lt;Router&gt;标签中:

      &lt;Router history={browserHistory}&gt;

    完整代码:-

    import React, { Component } from "react";
    import ReactDOM from "react-dom";
    //Imported this module for Browser history
    import { createBrowserHistory } from "history";
    import { Router } from "react-router-dom";
    
    import { Route, Link } from "react-router-dom";
    
    const browserHistory = createBrowserHistory();
    
    const topics = [
      {
        name: "Functional Programming",
        id: "functional-programming",
        description:
          "In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.",
        resources: [
          {
            name: "Imperative vs Declarative programming",
            id: "imperative-declarative",
            description:
              "A guide to understanding the difference between Imperative and Declarative programming.",
            url: "https://tylermcginnis.com/imperative-vs-declarative-programming/"
          },
          {
            name:
              "Building User Interfaces with Pure Functions and Function Composition",
            id: "fn-composition",
            description:
              "A guide to building UI with pure functions and function composition in React",
            url:
              "https://tylermcginnis.com/building-user-interfaces-with-pure-functions-and-function-composition-in-react-js/"
          }
        ]
      }
    ];
    
    const Resources = ({ match }) => {
      return <div>Resources</div>;
    };
    
    const Home = () => {
      return (
        <div>
          <h1>HOME </h1>
        </div>
      );
    };
    
    const Topic = ({ match }) => {
      console.log("match", match);
      const topic = topics.find(({ id }) => id === match.params.topicId);
      return (
        <div>
          <h1>{topic.name}</h1>
    
          <ul>
            {topic.resources.map(sub => (
              <li key={sub.id}>
                <Link to={`/topics/${match.params.topicId}/${sub.id}`}>
                  {sub.name}
                </Link>
              </li>
            ))}
          </ul>
          <hr />
          <Route path={`/topics/:topicId/:subId`} component={Resources} />
        </div>
      );
    };
    
    //Recieve the state
    class Topics extends React.Component {
      constructor(props) {
        super(props);
    
        this.state = {
          name: this.props.location.state.name
        };
      }
      render() {
        console.log("Hi");
        console.log("Topics location props", this.state.name);
        return (
          <div>
            <h1>Link data passed : {this.state.name}</h1>
            <h1>Topics </h1>
            <ul>
              {topics.map(({ name, id }) => (
                <li key={id}>
                  <Link to={`/topics/${id}`}> {name}</Link>
                </li>
              ))}
            </ul>
            <hr />
            <Route path={`/topics/:topicId`} component={Topic} />
          </div>
        );
      }
    }
    //Create state, include variable in it and send it.
    class App extends Component {
      state = {
        name: ""
      };
      render() {
        return (
          <Router history={browserHistory}>
            <div className="App">
              <h1>Nested Routers Learnings </h1>
              <ul>
                <li>
                  {" "}
                  <Link to="/">Home</Link>
                </li>
                <li>
                  {" "}
                  <Link
                    to={{
                      pathname: "/topics",
                      state: { name: "Nischith" }
                    }}
                  >
                    Topics
                  </Link>
                </li>
              </ul>
    
              <hr />
    
              <Route exact path="/" component={Home} />
              <Route path="/topics" component={Topics} />
            </div>
          </Router>
        );
      }
    }
    
    ReactDOM.render(<App />, document.querySelector("#root"));
    

    【讨论】:

    • 你的意思是你要先加载'/topics'路径(组件)??
    猜你喜欢
    • 2018-08-28
    • 2021-05-16
    • 2022-07-30
    • 2021-07-05
    • 2019-12-15
    • 2018-11-20
    • 2023-03-09
    • 2018-02-28
    • 1970-01-01
    相关资源
    最近更新 更多