E.g. let's say I have a hook for making an API request:
function useApi(...) {
const [state, setState] = useState({ fetching: false });
useEffect(() => {
setState({ fetching: true });
fetch(...)
.then(() => setState({ fetching: false }));
}, [...]);
return { fetching: state.fetching };
}
Sometimes, the state of useApi affects rendering:
function Foo() {
const { fetching } = useApi(...);
if (fetching) {
return 'Loading';
}
return 'Foo';
}
Other times, it doesn't affect rendering:
function Bar() {
useApi(...);
return 'Bar';
}
When setState in useApi doesn't affect rendering, is it possible to avoid re-rendering the component?
I know it doesn't affect performance much, but it's good to know if this is possible.