next it's a code that uses a HOC for showing some components while a fetch (with a custom hook) to an API REST is made.
From useSearchParams() I get the query string that will be used in fetching data.
The issue here is that when the component is rendered useFetch is executed but the search parameter (param) is not ready, then fetch is executed with null value.
const withLoading = (WrappedComponent) => {
const HocComponent = ({ ...props }) => {
const [urlParams, setUrlParams] = useSearchParams()
const [param, setParam] = useState()
useEffect(() => {
console.log(urlParams.get("text"));
setParam(urlParams.get("text"))
}, [urlParams])
const { data, loading, error } = useFetch("api_server", "/articles", { 'text': param })
if (loading) return (
<div className="grid grid-cols-6">
{Array(6).map(el => <ProductPulse />)}
</div>
)
return <WrappedComponent data={data} error={error} />
}
return HocComponent
}
Any comments on how to solve this issue using the hook useFetch?
I've tried many different ways without any luck.
I'm specting to use the custom hook useFetch in this scenario.