I have created a reusable component that is used by multiple consumer templates. And the templates are passing props with different values. template-A is passing propA = { name: "John"}
whereas template-B isn't passing propA. And the component makes an API call in the useEffect
method based on the passed propA where it sets a default value for propA as an empty object. the problem here is the template-B is invoking the API call twice. I assume it's causing because, since template-B isn't passing propA the value of propA changes from undefined to default empty object. How would you handle this scenario to avoid multiple API calls?
Component
const defaultVal = {};
const Component = ({ propA = defaultVal}) => {
useEffect(() => {
fetch("url").then((data) => {
setData(data)
})
}, [propA])
};
Template - A
<Component propA = {{ name: "John"}} />
Template - B
<Component />
Any helps would be appreciated, Thanks in advance!