Answer the question
In order to leave comments, you need to log in
Custom hooks or HOCs, when to use which?
Good afternoon, there are a bunch of components with similar functionality:
const CategoryPage = (props) => {
const [data, setData] = useState(null);
const { url, params: { category } } = useRouteMatch(); // #1
useEffect(() => {
CategoryService.get(params.category) // #2
.then(res => setData(res));
}, [category]);
return <PageComponent data={data} />;
}
const withPageData = (Component, selectData, routeParam) => {
return (props) => {
const [data, setData] = useState(null);
const { url, params } = useRouteMatch();
useEffect(() => {
selectData(params[routeParam])
.then(res => setData(res));
}, [url]);
return <Component data={data} {...props} />;
};
};
const usePageData = (selectData, routeParam) => {
const [data, setData] = useState(null);
const { url, params } = useRouteMatch();
useEffect(() => {
selectData(params[routeParam])
.then(res => setData(res));
}, [url]);
return data;
}
Answer the question
In order to leave comments, you need to log in
When to use HOC and when to use custom hooks?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question