Skip to content

Getting previous page from current location #520

Answered by molefrog
proxict asked this question in Q&A
Discussion options

You must be logged in to vote

I apologize in advance if this is a stupid question

There are no stupid questions! If I got the question right here is how I would solve this:

// PreviousLocationProvider.jsx
import { createContext, useContext } from "react";
import { useLocation } from "wouter";
import { usePrevious } from "@uidotdev/usehooks";

const PreviousLocationContext = createContext(null);

export const PreviousLocationProvider = ({ children }) => {
  const [location] = useLocation();
  const prevLocation = usePrevious(location);

  return (
    <PreviousLocationContext.Provider value={prevLocation}>
      {children}
    </PreviousLocationContext.Provider>
  );
};

export const usePreviousLocation = () => {
  r…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@molefrog
Comment options

@proxict
Comment options

Answer selected by molefrog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants