项目作者: zcallan

项目描述 :
A React hook to handle browser history events
高级语言: JavaScript
项目地址: git://github.com/zcallan/use-browser-history.git
创建时间: 2019-02-15T13:00:18Z
项目社区:https://github.com/zcallan/use-browser-history

开源协议:

下载


use-browser-history

A React hook to handle browser history events

NPM JavaScript Style Guide

https://zcallan.github.io/use-browser-history/

Install

  1. npm install --save use-browser-history

Usage

  1. import React, { useState } from 'react'
  2. import useBrowserHistory from 'use-browser-history'
  3. function Modal({ isOpen, onClose, onOpen }) {
  4. const [handleBack] = useBrowserHistory( 'my-modal', isOpen, onClose, onOpen );
  5. if ( !isOpen )
  6. return null;
  7. return (
  8. <div className="modal">
  9. <div className="modal-backdrop" onClick={handleBack} ></div>
  10. <div className="modal-box">
  11. <p>Hello!</p>
  12. <button onClick={handleBack}>Close modal</button>
  13. </div>
  14. </div>
  15. )
  16. }
  17. function App() {
  18. const [isOpen, setIsOpen] = useState( false );
  19. return (
  20. <div>
  21. <button onClick={() => setIsOpen( true )}>Open modal</button>
  22. <Modal
  23. isOpen={isOpen}
  24. onClose={() => setIsOpen( false )}
  25. onOpen={() => setIsOpen( true )}
  26. />
  27. </div>
  28. );
  29. }

API

  1. const [handleBack, handleForward] = useBrowserHistory( name, isActive, onBack, onForward );
Key Type Description
name string (required) The unique name to be used inside the browser history state.
isActive bool (required) Whether or not the entry should exist in the browser history state. For example, in the case of a modal, this should be set to true when the modal is open / visible, and false when it’s closed.
onBack func A callback that is fired when the back button is pressed, or when the handleBack (first arg returned by useBrowserHistory) function is called and the browser history state has been cleaned up.
onForward func Exactly like onBack, but for the forward button. You can use this to re-perform something if the user clicks back then forward (like in the example GIF above), to do something such as re-opening a modal.

License

MIT © zcallan