I have a basic component which displays list of users, when component is mounted I am using useEffect
to make an API call which will load the data, while data is loading I want to show loader in the UI.
My understanding is that the loader flag should remain in react component's state as it represents state of that component and not in redux store because redux store represents/saves state of entire application so like logged in user, list of users, etc. and not each possible state of a react component, is this kind of thinking wrong or incorrect from application's architecture point of view ?
My reasoning is that if we are going to store loading flags in redux store then why bother with using react component's state at all and not just use redux store?
If no, then what's the easiest way to achieve this ?
React Component
function List(props) {
const [loading, setLoading] = useState(false);
const reduxData = useSelector((store) => {
return {
users: store.users.list,
};
});
useEffect(() => {
//show loader
setLoading(true);
//wait until data is loaded
props.requestRequests();
// hide loader
setLoading(false);
}, []);
if (loading) {
return (
<Loader />
)
}
return (
<Table users={reduxData.users} />
);
}
setRequestStatus(requestId, REQUEST_STATUS.LOADING)
. Mix that with selector and you get a clean and flexible way to know each request status :). – Chandelle