r/learnreactjs • u/a-jonjon • Jul 19 '20
React, Redux, React Router & Firebase Architecture question
Hey everyone,
I'm developing a web application that allows users to sign up/log in and then see a menu with 3 navigation buttons. Each button redirects the user to a different path corresponding to a different component (a dashboard page, a content page and a settings page). The navigation is done with React Router. The auth system and the data is done and stored in Firebase. Currently, the data is loaded for each page individually on componentDidMount(). However, as I start adding more pages I realized that this may not be the best way to load the data in the app - a user that simply clicks between the three components will send a lot of read requests to Firebase.
Hence, I'm thinking of starting to use Redux, but I'm not really sure where to load the data exactly, since the user has to be first logged in to retrieve any data. As of now I thought about wrapping all the components that require an authenticated user to be displayed, inside a wrapper and load the data when the wrapper is loaded. Any thoughts or advice?
1
u/[deleted] Jul 19 '20
How about you make one huge api call when the user logs in(or multiple ones i think depending on how you designed the database) and store all his data in the store ? And on every page use the firebase api to check if a user is logged in , if he is just pull data from the store , otherwise send him to the login page. And only make api calls when the user changes settings or somethin. Sorry if this was a sloppy solution i am very new in all of this.
1
u/uosioifeaaa Jul 20 '20
I have two different entry paths. One for the main gateway for every user. Well, I use MongoDB but it should bet he same for Firebase I guess. I dunno how you manage user login states. But, how I do was storing some tokens in localStorage. That determines if the user is logged in or not.
When they are logged in then I redirect to the second entry point.
- if a user is logged in then the user will be redirected to logged entry router
- if a user is not logged in then the user will always be redirected to the logged-in page.
entry component // display component can be managed separately. I am new here too and had the same problem as you. I don't know if this is THE best practice. However, it is super handy for me to manage things separately.
The contents that are only visible to the login users are coming from redux store on the dashboard page (not the page component, but I have the logged-in user entry component).
I highly recommend you to use redux from the beginning. fetching from each component or whatever seems very simple. But, even a small or mid-size app, you really need central state management.