I am using a custom hook to load initial data into components. It works fine but I get a warning because I pass dynamically constructed dependencies to my useEffect hook: React Hook useEffect was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies
Here is the code for my custom hook:
const useApiData = (path: string, dependancies: any[], defaultValue: any) => {
const client = useApi();
const modelTemplate = {
isLoading: true,
data: defaultValue ? defaultValue : null
};
const [model, setModel] = useState(modelTemplate);
const dependencies: any[] = dependancies ? [...dependancies, client, path, model.isLoading] : [client, path, model.isLoading];
useEffect(() => {
if (!model.isLoading) {
setModel({ isLoading: true, data: null });
}
client.get(path).then(data => {
setModel({ isLoading: false, data: data });
}).catch(error => {
//do nothing, errors will be shown in notifications
});
}, dependencies);
return model;
}
And here is an example of passing an additional dependency:
const product = useApiData(`/products/${productId}`, [model.reviewAdded]);
In this component I am showing a product details and its reviews. I want to update the product details when a review gets added because the product rating gets changed. Can I do this with using my custom hook and not receiving a warning?
