useHistory.tsx 678 B

123456789101112131415161718192021222324252627282930
  1. import { useHistory } from 'umi';
  2. import { useEffect, useState } from 'react';
  3. import type { LocationState, Path } from 'history';
  4. import { model } from '@formily/reactive';
  5. export const historyStateModel = model<{ state: any }>({ state: {} });
  6. const useHistories = () => {
  7. const umiHistory = useHistory();
  8. const [history, setHistory] = useState<any>();
  9. const push = (location: Path, state?: LocationState) => {
  10. if (state) {
  11. historyStateModel.state[location] = state;
  12. }
  13. umiHistory.push(location, state);
  14. };
  15. useEffect(() => {
  16. setHistory({
  17. ...umiHistory,
  18. push,
  19. });
  20. }, []);
  21. return history;
  22. };
  23. export default useHistories;