Next.js Discord

Discord Forum

Extracted ID from URL: undefined

Answered
Jordi's Bodyguard posted this in #help-forum
Open in Discord
when i use numbers it work but when i use high-school-dxd-1834 it didnt work. how to fix?

import React, { useEffect, useState } from 'react';
import axios from 'axios';
import { Typography, Card, CardMedia, CardContent, CircularProgress } from '@mui/material';
import { useParams } from 'react-router-dom';

const AnimeInfo = () => {
  const { id } = useParams(); 
  const [anime, setAnime] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    console.log(`Extracted ID from URL: ${id}`); // Log the ID

    if (id) {
      const fetchAnimeInfo = async () => {
        try {
          const response = await axios.get(`https://mellieapi2.vercel.app/anime/info?id=${id}`);
          setAnime(response.data);
          setLoading(false);
        } catch (error) {
          setError(error);
          setLoading(false);
        }
      };

      fetchAnimeInfo();
    } else {
      setLoading(false);
      setError('Invalid ID');
    }
  }, [id]);

  if (loading) return <CircularProgress />;
  if (error) return <Typography variant="body1" color="error">Error fetching anime info: {error.toString()}</Typography>;
  if (!anime) return null;

  return (
    <Card>
      <CardMedia
        component="img"
        height="auto"
        image={anime.poster}
        alt={`${anime.name} poster`}
      />
      <CardContent>
        <Typography variant="h4">{anime.name}</Typography>
        <Typography variant="body1" paragraph>
          {anime.description}
        </Typography>
        <Typography variant="body2">Rating: {anime.stats.rating}</Typography>
        <Typography variant="body2">Sub Episodes: {anime.stats.episodes.sub}</Typography>
        <Typography variant="body2">Dub Episodes: {anime.stats.episodes.dub}</Typography>
        <Typography variant="body2">Duration: {anime.stats.duration}</Typography>
      </CardContent>
    </Card>
  );
};

export default AnimeInfo;
Answered by B33fb0n3
you are trying to use [useParams](https://nextjs.org/docs/app/api-reference/functions/use-params) but imported it from the wrong location. So replace like:
- import { useParams } from 'react-router-dom';
+ import { useParams } from 'next/navigation'
View full answer

3 Replies

@Jordi's Bodyguard when i use numbers it work but when i use high-school-dxd-1834 it didnt work. how to fix? js import React, { useEffect, useState } from 'react'; import axios from 'axios'; import { Typography, Card, CardMedia, CardContent, CircularProgress } from '@mui/material'; import { useParams } from 'react-router-dom'; const AnimeInfo = () => { const { id } = useParams(); const [anime, setAnime] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { console.log(`Extracted ID from URL: ${id}`); // Log the ID if (id) { const fetchAnimeInfo = async () => { try { const response = await axios.get(`https://mellieapi2.vercel.app/anime/info?id=${id}`); setAnime(response.data); setLoading(false); } catch (error) { setError(error); setLoading(false); } }; fetchAnimeInfo(); } else { setLoading(false); setError('Invalid ID'); } }, [id]); if (loading) return <CircularProgress />; if (error) return <Typography variant="body1" color="error">Error fetching anime info: {error.toString()}</Typography>; if (!anime) return null; return ( <Card> <CardMedia component="img" height="auto" image={anime.poster} alt={`${anime.name} poster`} /> <CardContent> <Typography variant="h4">{anime.name}</Typography> <Typography variant="body1" paragraph> {anime.description} </Typography> <Typography variant="body2">Rating: {anime.stats.rating}</Typography> <Typography variant="body2">Sub Episodes: {anime.stats.episodes.sub}</Typography> <Typography variant="body2">Dub Episodes: {anime.stats.episodes.dub}</Typography> <Typography variant="body2">Duration: {anime.stats.duration}</Typography> </CardContent> </Card> ); }; export default AnimeInfo;
you are trying to use [useParams](https://nextjs.org/docs/app/api-reference/functions/use-params) but imported it from the wrong location. So replace like:
- import { useParams } from 'react-router-dom';
+ import { useParams } from 'next/navigation'
Answer
tho I wonder why there is react-router-dom in a next.js project