Skip to content

Commit

Permalink
Replacing the depreciated useHistory() hook with the new useNavigate(…
Browse files Browse the repository at this point in the history
…) hook

This updates the navigation logic to align with React Router v6, replacing the deprecated useHistory() hook with the new useNavigate() hook. React Router v6 introduced useNavigate() to simplify programmatic navigation and remove the legacy useHistory() API. This change ensures the application works seamlessly with React Router v6.

Changes include:

Replaced useHistory() with useNavigate() in the Tutorials component and relevant functions.
Updated navigation calls from history.push() to navigate() for navigating to tutorial pages and selecting tags.
Refactored handleContainerClick and selectTag to use navigate() for URL updates and navigation.
This change ensures the codebase is compatible with the latest version of React Router, improving readability and future-proofing the app.
  • Loading branch information
valentineejimole authored Dec 21, 2024
1 parent 03b3fc5 commit 6c3199f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions apps/base-docs/src/pages/tutorials/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import clsx from 'clsx';
import styles from './styles.module.css';
import tutorialData from '../../../tutorials/data.json';
import authors from '@app/base-docs/static/json/authors.json';
import { useHistory, useLocation } from 'react-router-dom';
import { useNavigate, useLocation } from 'react-router-dom';

const TagList = [
'all',
Expand Down Expand Up @@ -47,23 +47,23 @@ function TagChip({ tag, isSelected, setSelectedTag }) {
);
}

const handleContainerClick = (event, tutorial, history) => {
const handleContainerClick = (event, tutorial, navigate) => {
if (event.target.closest('a')) return;
history.push(`/tutorials${tutorial.slug}`);
navigate(`/tutorials${tutorial.slug}`);
};

const handleClick = (event) => {
event.stopPropagation();
};

function TutorialListCell({ tutorial }) {
const history = useHistory();
const navigate = useNavigate();
const authorData = authors[tutorial.author];
return (
// <Link to={`/tutorials${tutorial.slug}`}>
<div
onClick={(event) => {
handleContainerClick(event, tutorial, history);
handleContainerClick(event, tutorial, navigate);
}}
role="button"
tabIndex={0}
Expand Down Expand Up @@ -105,7 +105,7 @@ const TITLE = 'Base Builder Tutorials';

export default function Tutorials() {
const [selectedTag, setSelectedTag] = useState('all');
const history = useHistory();
const navigate = useNavigate();
const query = useQuery();

useEffect(() => {
Expand All @@ -117,9 +117,9 @@ export default function Tutorials() {

const selectTag = useCallback(
(tag) => {
history.push(`?tag=${tag}`);
navigate(`?tag=${tag}`);
},
[history],
[navigate],
);

return (
Expand Down

0 comments on commit 6c3199f

Please sign in to comment.