简体   繁体   English

react-router-dom 单击关闭引导模式的链接 window

[英]react-router-dom Link on click close bootstrap modal window

I need to close the modal window by clicking on the link.我需要通过单击链接关闭模式 window。 My modal window:我的模式 window:

export const ModalWrapper = (props) => {
  const { children, title, modalName } = props;
  return (
    <div
      className="modal fade"
      id={`modal-${modalName}`}
      tabIndex="-1"
      role="dialog"
      aria-labelledby="modal-label"
      aria-hidden="true"
      style={{ display: 'none' }}
    >
      <div className="modal-dialog modal">
        <div className="modal-content" style={{ overflow: 'hidden' }}>
          <div className="modal-header">
            { title }
            <button type="button" className="close" data-dismiss="modal" aria-hidden="true">×</button>

          </div>
          <div className="modal-body" style={{ maxHeight: '435', overflowY: 'auto' }}>
            <ul className="list-unstyled todo-list">
              {children}
            </ul>
          </div>
        </div>
      </div>
    </div>
  );
};

How can I close window with react-router-dom Link?如何使用 react-router-dom 链接关闭 window?

It close window without redirect:它在没有重定向的情况下关闭 window:

<Link to="/registration" data-dismiss="modal" className="btn btn-default">Registration</Link>

It redirects but the window does not close:它重定向但 window 没有关闭:

<Link to="/registration" className="btn btn-default">Registration</Link>

Thank you!谢谢!

To achieve your goal you can either: 要实现您的目标,您可以:

You can read more about React Bootstrap library at their website. 您可以在其网站上阅读有关React Bootstrap库的更多信息。

If you want to avoid extra dependencies, you can use React's ref functionality: https://reactjs.org/docs/refs-and-the-dom.html and use it to manipulate the dom (eg hide the dialog on Link click or any other action). 如果要避免额外的依赖关系,可以使用React的ref功能: https : //reactjs.org/docs/refs-and-the-dom.html并使用它来操作dom(例如,隐藏Link单击或任何其他动作)。 In this case simply attach ref callback function to the dialog dom element: 在这种情况下,只需将ref回调函数附加到对话框dom元素:

<div className="modal" ref={(input) => {this.input = input}}>
...

In this case you can access dom's className and remove the show class from it. 在这种情况下,您可以访问dom的className并从中删除show类。 Be aware that you will also have to remove show from the fade container (dimmed background generated by Bootstrap). 请注意,您还必须从淡入淡出的容器(由Bootstrap生成的背景变暗)中删除显示。

My overall suggestion is to use utility function that will hide current modal and fade. 我的总体建议是使用实用程序功能,它将隐藏当前的模态和淡入淡出。 Smth like: 像:

const hideActiveModal = () => {
  const modal = document.getElementsByClassName('modal show')[0];
  const fade = document.getElementsByClassName('modal-backdrop show')[0];
  modal.className = modal.className.replace('show', '');
  fade.className = fade.className.replace('show', '');
};

Conclusion: you can't hide Bootstrap using clear React at moment. 结论:您现在无法使用清晰的React隐藏Bootstrap。 Either use React-Bootstrap or use util function (like the one I have provided). 使用React-Bootstrap或使用util函数(如我提供的函数)。

As an update, this can now be solved by monitoring the location of the router in React Router V6 and combining with useRef() to set the initial location:作为更新,现在可以通过在 React Router V6 中监控路由器的位置并结合useRef()来设置初始位置来解决这个问题:

    const location = useLocation();
    const [showModal, setShowModal] = useState(true);

    const initialPage = useRef(location.pathname);

    useEffect(() => {
        if (location.pathname !== initialPage.current) {
            setShowModal(false);
        }
    }, [location]);

I'm using React Bootstrap, but for your current implementation, you could update the relevant classNames:我正在使用 React Bootstrap,但对于您当前的实现,您可以更新相关的类名:

className={`modal ${showModal? 'show', ''}`}

You'd also need to update the close button:您还需要更新关闭按钮:

   onClick={() => {
      setShowModal(false);
   }}

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

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