0

i have this problem:

 Line 25:7:  React Hook useEffect has missing dependencies: 'getSingleProductData', 'isProductOnSale', and 'productData'. Either include them or remove the dependency array  react-hooks/exhaustive-deps

this is the code:

    type SingleProductParams = {
    s_id:string;
}

type SingleProductProps = RouteComponentProps<SingleProductParams>

const SingleProduct: React.FC<SingleProductProps> = (props) => {
    let [productData, setProductData] = useState<Partial<IProdItems>>({});
    let [isProductOnSale, setIsProductOnSale] = useState<Sale['onSale']>();

    useEffect( () => { 
        setIsProductOnSale(productData.sale?.onSale);
        getSingleProductData();
        console.log(productData);
        console.log(isProductOnSale);
        console.log(props.match.params.s_id);
    },[props]) 
    
    const getSingleProductData = async() => { 
        let url = URL_API + "/products/single/" + props.match.params.s_id;
        let data = await doApiGet(url);
        console.log(data);
        setProductData(data);
    }

how can I fix it? i tried every way from google, that's just doesn't work. but the page all work, i get the data and it's all fine. should i disable the error? I must mention that the console logs inside the useEffect is undefined but the data shown in the site!

1 Answer 1

2

The warning means that you need to add getSingleProductData, isProductOnSale and productData to useEffect's deps list in this way:

useEffect( () => { 
    setIsProductOnSale(productData.sale?.onSale);
    getSingleProductData();
    console.log(productData);
    console.log(isProductOnSale);
    console.log(props.match.params.s_id);
},[props, getSingleProductData, isProductOnSale, productData]) //<-- here add them

And warning disappears.

EDIT

By adding these dependencies you could fall into the loop problem (useEffect has a dependency that will be updated in useEffect itself).

In this case you could initialize multiple useEffects, everyone with needed dependencies:

useEffect(() => {  //<-- this useEffect to set productData (it runs just one time at component loading)
    getSingleProductData(); 
},[]) 

useEffect(() => { //<-- this useEffect to set isProductOnSale every time productData changes 
   setIsProductOnSale(productData.sale?.onSale);
}, [productData])

useEffect(() => { //<-- this useEffect print stuff
    console.log(productData);
    console.log(isProductOnSale);
    console.log(props.match.params.s_id);
}, [productData, isProductOnSale, props])
Sign up to request clarification or add additional context in comments.

5 Comments

diappears = disappears. (can't added as it needs atleast 6 charachters). But this is the solution, was going to answer the same. :-)
yea that doesn't work, it just render the data every time. and it doesn't let me add a function to the deps list.
@Aluf dependencies problem will be solved like this. You could have another problem: if some function in useEffect modifies one or more elements in deps list then you will have a loop. Is this the case?
yea that's the case, it keeps render the data with a loop. @GiovanniEsposito
hey that half worked, when i try your edit of code it keep on giving me the error about the function: Line 22:7: React Hook useEffect has a missing dependency: 'getSingleProductData' and when I pass it to one of the deps it rendering loop the function. got any ideas? :( @GiovanniEsposito

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.