【问题标题】:React Router v4 Modal反应路由器 v4 模式
【发布时间】:2017-02-23 07:09:26
【问题描述】:

我知道如何在以前版本的 React Router 中执行此操作,但我绝对不知道如何在新的 React Router v4 中执行此操作。有人可以帮帮我吗?

我想要什么?

  1. 当您在浏览器 url 中输入 /image/1 时,页面将正常显示。
  2. 当你点击状态为modal: true<Link>Image as modal</Link>时,会出现带图片的模态框,但模态框后面必须是之前的内容+浏览器中的url === /image/1...然后如果你按F5,页面将正常显示。

例如:instagram...等

我认为我做错了什么?

  1. 不知道如何显示以前的内容。我猜就是这样。

代码:

const Images = (props) => {
  return (
    <div>
      <h2>Images</h2>
      <ul>
        <li><Link to={{
          pathname: '/image/1',
          state: {
            modal: true
          }
        }}>Image as modal</Link></li>
        <li><Link to="/image/2">Image</Link></li>
      </ul>
    </div>
  )
}

const Image = (props) => {
  return (
    <div>
      <h2>Image {props.match.params.id}</h2>
      <ul>
        <li><Link to="/images">Back to Images</Link></li>
      </ul>
    </div>
  )
}

ReactDOM.render(
  <Provider store={store}>
    <Router>
      <div>
        <Route path='/images' component={Images} />
        <Route path='/image/:id' component={Image} />
      </div>
    </Router>
  </Provider>,
  document.getElementById('digital')
)

【问题讨论】:

标签: javascript reactjs react-router


【解决方案1】:

我遇到了同样的问题,所以我创建了:

http://npmjs.com/package/react-router-modal

它允许您将模式附加到路线。您可以按如下方式使用它:

import { ModalContainer, ModalRoute } from 'react-router-modal';
// ... 
ReactDOM.render(
  <Provider store={store}>
    <Router>
      <div>
        <Route path='/images' component={Images} />
        <ModalRoute path='/image/:id' component={Image} />
      </div>
    </Router>
    <ModalContainer />
  </Provider>,
  document.getElementById('digital')
)

https://davidmfoley.github.io/react-router-modal-examples/有几个简单的例子

希望对你有帮助。

【讨论】:

  • 请提供一些代码/解释如何使用这个包。查看How to Answer,了解如何处理指向其他页面的链接的详细信息。
【解决方案2】:

我能够使用here 描述的解决方案,它指的是这个code example

import React from "react";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";

// This example shows how to render two different screens
// (or the same screen in a different context) at the same url,
// depending on how you got there.
//
// Click the colors and see them full screen, then "visit the
// gallery" and click on the colors. Note the URL and the component
// are the same as before but now we see them inside a modal
// on top of the old screen.

class ModalSwitch extends React.Component {
  // We can pass a location to <Switch/> that will tell it to
  // ignore the router's current location and use the location
  // prop instead.
  //
  // We can also use "location state" to tell the app the user
  // wants to go to `/img/2` in a modal, rather than as the
  // main page, keeping the gallery visible behind it.
  //
  // Normally, `/img/2` wouldn't match the gallery at `/`.
  // So, to get both screens to render, we can save the old
  // location and pass it to Switch, so it will think the location
  // is still `/` even though its `/img/2`.
  previousLocation = this.props.location;

  componentWillUpdate(nextProps) {
    let { location } = this.props;

    // set previousLocation if props.location is not modal
    if (
      nextProps.history.action !== "POP" &&
      (!location.state || !location.state.modal)
    ) {
      this.previousLocation = this.props.location;
    }
  }

  render() {
    let { location } = this.props;

    let isModal = !!(
      location.state &&
      location.state.modal &&
      this.previousLocation !== location
    ); // not initial render

    return (
      <div>
        <Switch location={isModal ? this.previousLocation : location}>
          <Route exact path="/" component={Home} />
          <Route path="/gallery" component={Gallery} />
          <Route path="/img/:id" component={ImageView} />
        </Switch>
        {isModal ? <Route path="/img/:id" component={Modal} /> : null}
      </div>
    );
  }
}

const IMAGES = [
  { id: 0, title: "Dark Orchid", color: "DarkOrchid" },
  { id: 1, title: "Lime Green", color: "LimeGreen" },
  { id: 2, title: "Tomato", color: "Tomato" },
  { id: 3, title: "Seven Ate Nine", color: "#789" },
  { id: 4, title: "Crimson", color: "Crimson" }
];

function Thumbnail({ color }) {
  return (
    <div
      style={{
        width: 50,
        height: 50,
        background: color
      }}
    />
  );
}

function Image({ color }) {
  return (
    <div
      style={{
        width: "100%",
        height: 400,
        background: color
      }}
    />
  );
}

function Home() {
  return (
    <div>
      <Link to="/gallery">Visit the Gallery</Link>
      <h2>Featured Images</h2>
      <ul>
        <li>
          <Link to="/img/2">Tomato</Link>
        </li>
        <li>
          <Link to="/img/4">Crimson</Link>
        </li>
      </ul>
    </div>
  );
}

function Gallery() {
  return (
    <div>
      {IMAGES.map(i => (
        <Link
          key={i.id}
          to={{
            pathname: `/img/${i.id}`,
            // this is the trick!
            state: { modal: true }
          }}
        >
          <Thumbnail color={i.color} />
          <p>{i.title}</p>
        </Link>
      ))}
    </div>
  );
}

function ImageView({ match }) {
  let image = IMAGES[parseInt(match.params.id, 10)];

  if (!image) return <div>Image not found</div>;

  return (
    <div>
      <h1>{image.title}</h1>
      <Image color={image.color} />
    </div>
  );
}

function Modal({ match, history }) {
  let image = IMAGES[parseInt(match.params.id, 10)];

  if (!image) return null;

  let back = e => {
    e.stopPropagation();
    history.goBack();
  };

  return (
    <div
      onClick={back}
      style={{
        position: "absolute",
        top: 0,
        left: 0,
        bottom: 0,
        right: 0,
        background: "rgba(0, 0, 0, 0.15)"
      }}
    >
      <div
        className="modal"
        style={{
          position: "absolute",
          background: "#fff",
          top: 25,
          left: "10%",
          right: "10%",
          padding: 15,
          border: "2px solid #444"
        }}
      >
        <h1>{image.title}</h1>
        <Image color={image.color} />
        <button type="button" onClick={back}>
          Close
        </button>
      </div>
    </div>
  );
}

function ModalGallery() {
  return (
    <Router>
      <Route component={ModalSwitch} />
    </Router>
  );
}

export default ModalGallery;

【讨论】:

  • 但问题是它在刷新时不显示模态......
【解决方案3】:

一个简单的带有 javascript 的模态路由示例,可以与任何路由系统一起使用

<button onClick={() => {
    this.setState({ modal: true });
    window.history.pushState("","","/gallery/image/img_1233")
}}>
    Open Modal
</button>

//Link Button
<Link href="/gallery/image/img_1233">
    <a>Open Page</a>
</Link>

此处的完整示例:https://github.com/mohammad-amin-hesam/react-modal-route-example

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 2017-12-06
    • 2017-09-16
    • 2017-09-03
    • 2018-07-07
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    相关资源
    最近更新 更多