I have a <Loading /> component that wraps all routes in my app.
The <Loading /> component has a property isLoading that receives a Redux state variable, that tells if the Loading component will be visible or not.
function App() {
const isLoading = useSelector((state) => state.loading.value)
return (
<Loading isLoading={isLoading}>
<BrowserRouter>
<Routes>
<Route path='/' element={<Hypertube />} />
<Route path='/login' element={<Login />} />
<Route path='/register' element={<Register />} />
</Routes>
</BrowserRouter>
</Loading>
);
}
When the user opens a page, I need to change the Redux state to true, so the <Loading /> component will be visible (and then hide it when the page gets rendered).
So what I need, is to change the state before the functional component gets rendered (or while the component is rendering).
I wanted to use componentWillMount(), but it's deprecated now.
A solution to this (that I don't like) is to do this 2 changes:
- Set the default redux state value to true, so we can see the loading whenever the page get opened for the first time.
- Change the state value to
truewhenever the user get redirected to another page.
true?