0

import { useParams } from 'react-router-dom';
import useFetch from './useFetch';

const BlogDetails = () => {
    const { id } = useParams();
    const { data: blog, error, isPending } = useFetch('http://localhost:3001/blogs/' + id);

    return (
        <div className="blog-details">
            { isPending && <div>...Loading...</div>}
            { error && <div>{error}</div>}
            { blog && (
                <article>
                    <h2>{blog.title}</h2>
                    <p>Written by {blog.author}</p>
                    <div>{blog.body}</div>
                </article>
            )}
        </div>
    );
}

export default BlogDetails;

import {useState, useEffect} from 'react';

const useFetch = (url) => {
        const [data, setData] = useState(null);
        const [isPending, setIsPending] = useState(true);
        const [error, setError] = useState(null);

    useEffect(() => {
        const abortCont = new AbortController();

        fetch(url, { signal: abortCont.signal })
            .then(res => {
                if(!res.ok){
                    throw Error('404 - data not found!');
                } 
                return res.json();
            })
            .then(data => {
                setData(data);
                setIsPending(false);
                setError(null);
            })
            .catch(err => {
                if (err.name === 'AbortError') {
                    console.log('fetch aborted');
                } else {
                    setIsPending(false);
                    setError(err.message);
                }
            })

            return () => abortCont.abort();
    }, [url]);

    return {data, isPending, error}
}

export default useFetch;

I am fairly new to react and i have spent about 2 days searching, adding and removing packages on VSCode to no avail.

I am following the Net Ninja's recent tutorial on Youtube and I am stuck on step #25. (https://www.youtube.com/watch?v=t7VmF4WsLCo&t=4s)

I have copied the code exactly but the issue seems somewhere else in React.

If anyone could help or point me in the right direction, it would be greatly appreciated.

This is the error that is giving each time I try to access the page. It is thrown for any of the pages that is not on the main page.

A. Caddle
  • 1
  • 4
  • 3
    Hi Caddle, You need to provide some more details to people help you out. Your error points to `useParams` hooks called at `BlogDetails`, thus you should consider adding these pieces of your code to your question. Use the tools to format it the code properly. Please, don't print and paste the code. – buzatto Feb 10 '21 at 21:13
  • Apologize, i have added the BlogDetails page and the useFetch page. I am still getting my head around the understanding of Hooks and got lost googling answers for this issue. Any help will be greatly appreciated. – A. Caddle Feb 11 '21 at 09:30
  • the piece of code is fine. the error is triggered by `useParams` which is a stable hook from `react-dom`, and you are calling at a function component which is correct. I believe you are calling `BlogDetails` correctly also. it's more likely the error due to issues regarding installed react packages, like the error points out. – buzatto Feb 11 '21 at 13:33
  • 1
    Hi, I was wondering if that may be the issue. I was thinking of re-starting the project in a fresh file and re-installing the packages as needed if that would have any benefit. All the versions seemed to be up to date but I will double-check later and re-deploy. Thanks for your advice, very appreciated. – A. Caddle Feb 11 '21 at 14:35

0 Answers0