Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
Problem displaying an item according to the url - React

I have a question. I have a component that when entering /category/:categoryId is rendered doing a fecth to "api url + categoryId". My problem is that if I change from one category to another the page only changes if the useEffect is executed infinitely which generates problems to the view as seen below. How can I make it run once and when I change from /category/1 to /category/2 the useEffect is executed correctly?

const Categories = () => {
    let [producto, productos] = useState([]);
    const { categoryId } = useParams();

    useEffect(() => {
        fetch('https://fakestoreapi.com/products/category/' + categoryId)
            .then(res=>res.json())
            .then(data=>productos(data))
    },[]);

    console.log(producto)

    return(
        <div className="container">
            {producto.map((p) => (
                <Producto
                    title={p.title}
                    price={p.price}
                    description={p.description}
                    image={p.image}
                    key={p.id}
                    id={p.id}
                />
            ))}
        </div>
    )
}

export default Categories;

My router file:

<Route path="/category/:categoryId" component={Categories} />

This is the problem that is generated, there comes a time when a fetch is made to a previously requested category and then the new requested category is executed.

See my problem in video

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can simply add categoryId to useEffect array argument. Function inside the useEffect is called, only when categoryId changes

    useEffect(() => {
        fetch('https://fakestoreapi.com/products/category/' + categoryId)
            .then(res=>res.json())
            .then(data=>productos(data))
    },[categoryId]);
about 4 years ago · Juan Pablo Isaza Report

0

you can not edit producto directly, you should use productos :

const Categories = () => {
    let [producto, productos] = useState([]);
    const { categoryId } = useParams();

    useEffect(() => {
        fetch('https://fakestoreapi.com/products/category/' + categoryId)
            .then(res=>res.json())
            .then(data=>productos(data))
    },[]);

    console.log(producto)

    return(
        <div className="container">
            {producto && producto.map((p) => (
                <Producto
                    title={p.title}
                    price={p.price}
                    description={p.description}
                    image={p.image}
                    key={p.id}
                    id={p.id}
                />
            ))}
        </div>
    )
}

export default Categories;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!