MemoryRouter.js 981 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import { createMemoryHistory as createHistory } from "history";
  4. import warning from "tiny-warning";
  5. import Router from "./Router.js";
  6. /**
  7. * The public API for a <Router> that stores location in memory.
  8. */
  9. class MemoryRouter extends React.Component {
  10. history = createHistory(this.props);
  11. render() {
  12. return <Router history={this.history} children={this.props.children} />;
  13. }
  14. }
  15. if (__DEV__) {
  16. MemoryRouter.propTypes = {
  17. initialEntries: PropTypes.array,
  18. initialIndex: PropTypes.number,
  19. getUserConfirmation: PropTypes.func,
  20. keyLength: PropTypes.number,
  21. children: PropTypes.node
  22. };
  23. MemoryRouter.prototype.componentDidMount = function() {
  24. warning(
  25. !this.props.history,
  26. "<MemoryRouter> ignores the history prop. To use a custom history, " +
  27. "use `import { Router }` instead of `import { MemoryRouter as Router }`."
  28. );
  29. };
  30. }
  31. export default MemoryRouter;