Property 'background' does not exist on type 'unknown'. ReactJS/useHistory()

Viewed 202

I'm getting this error Property 'background' does not exist on type 'unknown'.ts(2339), when I try to use this code

const location = useLocation();
const background = location.state && location.state.background;

I imported all of the needed functions from react-router-dom

import {
  Redirect,
  Route,
  RouteComponentProps,
  Switch,
  useLocation,
  withRouter,
} from "react-router-dom";

I'm using class component if it does matter.

I will be very grateful if you could give me some tips.

1 Answers
import {Location} from "history";
const location = useLocation<{background?: Location<{} | null | undefined>}>();
Related