In my func component I am trying to use history.push
in this way:
import React, { useEffect } from 'react';
import { useHistory } from 'react-router-dom';
interface Props {
question: Question;
selectedAnswer: SelectedOption | undefined;
}
const LastScreen: React.FC<Props> = (props: Props) => {
const history = useHistory();
...
useEffect(() => {
if (progress === 100) {
const id = uuid();
history.push({
pathname: `/p/${id}`,
state: { userAnswers },
});
}
}, [progress, userAnswers, history]);
...
};
but it gives an error:
Argument of type '{ pathname: string; state: { userAnswers: UserAnswers | undefined; }; }' is not assignable to parameter of type 'To'. Object literal may only specify known properties, and 'state' does not exist in type 'PartialPath'.ts(2345)
How to fix it?
import history from 'history';
and then<Router history={history}>...</Router>
So when I replaced it with<BrowserRouter>...</BrowserRouter>
the problem disappeared. – AmigouseHistory
is old (v5), now (v6),const navigate = useNavigate(); navigate('/')
is used. Reference: https://mcmap.net/q/378009/-how-to-properly-use-usehistory-from-react-router-dom – Zildjian