【问题标题】:React-router-modal strange behavior - I need two clicks to go back, should be just oneReact-router-modal 奇怪的行为 - 我需要点击两次才能返回,应该只是一次
【发布时间】:2018-03-18 17:03:43
【问题描述】:

我使用了这个 npm 包 => react-router-modal(doc)

这里 (click) 在该示例中只需单击一次即可从模态返回,但在我的情况下,我必须单击鼠标 两次 才能返回,然后我不知道为什么...

所以也许这里有人可以帮我回答这个问题。

该问题的快速演示 -> https://youtu.be/szRC_K10pyA

完整的项目代码 -> github

以下两个最重要的组件:

组件BeerListItem

import React, { Component } from 'react';
import Card, { CardContent } from 'material-ui/Card';
import { ModalContainer, ModalRoute } from 'react-router-modal';
import { BrowserRouter, Link } from 'react-router-dom';
import 'react-router-modal/css/react-router-modal.css';
import './style.css';

import BeerProfile from '../BeerProfile';

class BeerListItem extends Component {
  constructor(props) {
    super(props);

    this.beerProfile = this.beerProfile.bind(this);
  }

  beerProfile() {
    const { beer } = this.props;
    return (
      <div>
        <BeerProfile beer={beer} />
      </div>
    );
  }

  render() {
    const { beer } = this.props;
    let cutStr = '';

    if (beer.name.length >= 27) {
      cutStr = `${beer.name.slice(0, 26)}...`;
    } else {
      cutStr = beer.name;
    }

    return (
      <BrowserRouter>
        <div>
          <Link to={`/details/${beer.id}`}>
            <Card raised className="BeerListItem-main-card">
              <CardContent>
                <img
                  src={beer.image_url}
                  alt="beer"
                  className="BeerListItem-img"
                />
                <div className="BeerListItem-h2-and-p-container">
                  <h2 className="BeerListItem-h2">{cutStr}</h2>
                  <p className="BeerListItem-p">{beer.tagline}</p>
                </div>
              </CardContent>
            </Card>
          </Link>

          <ModalRoute
            className="test-modal test-modal-foo"
            path={`/details/${beer.id}`}
            parentPath="/"
            component={this.beerProfile}
          />

          <ModalContainer />
        </div>
      </BrowserRouter>
    );
  }
}

export default BeerListItem;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

组件BeerProfile

import React, { Component } from 'react';
import { Modal } from 'react-router-modal';
import 'react-router-modal/css/react-router-modal.css';
import './style.css';

class BeerProfile extends Component {
  constructor(props) {
    super(props);

    this.state = {
      show: true,
    };
    this.handleBackClick = this.handleBackClick.bind(this);
  }

  handleBackClick() {
    this.setState({ show: false });
  }

  render() {
    console.log(this.props);

    const { id } = this.props.beer;
    return (
      <div>
        {this.state.show && (
          <Modal onBackdropClick={this.handleBackClick}>
            <h3>Child elements</h3>
            <p>{id}</p>
          </Modal>
        )}
      </div>
    );
  }
}

export default BeerProfile;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

【问题讨论】:

    标签: javascript css reactjs react-router react-modal


    【解决方案1】:

    我想我明白了。

    所以当你的模态被渲染时,这是我得到的控制台日志,

    所以基本上,会发生两次安装而不是一次。

    当我检查代码库时,我看到在 BeerListItem 内部,您使用了包中的 ModalRoute 组件。

     <ModalRoute
       className="test-modal test-modal-foo"
       path={`/details/${beer.id}`}
       parentPath="/"
       component={this.beerProfile}
     />
    

    this.beerProfile 呈现以下内容

    {this.state.show && (
      <Modal onBackdropClick={this.handleBackClick}>
        <h3>Child elements</h3>
        <p>{id}</p>
      </Modal>
    

    Modal 也来自包本身

    来自文档 => ModalRoute

    ModalRoute 一个 react-router 路由,当位置显示模态 路径名匹配。

    => Modal

    Modal 在带有背景的模态 div 中呈现其内容。使用模态 如果您想在不更改路线的情况下显示模式。

    所以基本上这是一个非此即彼的情况。如果你想要一个带有不断变化的 url 的模式,你应该使用ModalRoute。如果你只想要模态,你应该使用Modal

    据我了解,当您将 component 属性传递给 ModalRoute 时,它会自动使用 Modal 包装此组件

    所以你需要做的是,替换

    <div>
      {this.state.show && (
        <Modal onBackdropClick={this.handleBackClick}>
          <h3>Child elements</h3>
          <p>{id}</p>
         </Modal>
       )}
     </div>
    

      <div>
        <h3>Child elements</h3>
        <p>{id}</p>
      </div>
    

    啊还有一件小事,你还需要摆脱className

    <ModalRoute
                className="test-modal test-modal-foo"
    

    当我检查代码库时,我找不到与这两个相关的样式。

    在这些更改之后,应该可以按您预期的方式工作。

    一个提示,ModalRoute 也接受 props 道具。因此,您可以执行以下操作,而不是传递 this.beerProfile,它也应该可以工作

    <ModalRoute
      path={`/details/${beer.id}`}
      props={this.props}
      parentPath="/"
      component={BeerProfile}
    />
    

    目前在我的本地,它按预期工作。请尝试让我知道它是否也适合您。

    【讨论】:

    • 很好的答案!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2016-06-08
    • 2022-07-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多