This example shows using data from redux state to make a query based on props (also known as a dynamic query). A good example of this is querying based on the current user's UID.
Note: Example does not use routing, which is what will commonly be used when creating a full application. For how to build with routing, please view the routing recipes section of the docs.
-
Top level component in
Home.js
usesconnect
function to bringauth
from redux state into a prop name "auth":// Map redux state to props export default connect(state => ({ auth: state.firebase.auth }))(Home)
-
That component then uses
isLoaded
andisEmpty
to show different views based on auth state (logged in or not):function Home({ auth }) { // handle initial loading of auth if (!isLoaded(auth)) { return <div>Loading...</div> } // User is not logged in, show login view if (isEmpty(auth)) { return <LoginView /> } // Pass uid to Todos (later used for a listener) return <Todos uid={auth.uid} /> }
-
The component in
Todos.js
then usesuid
(from props) as part of a query for todos:const enhance = compose( // Create Real Time Database Listeners on mount firebaseConnect(props => [ // uid comes from props { path: 'todos', queryParams: ['orderByChild=uid', `equalTo=${props.uid}`] } ]), // Map redux state to props connect(state => ({ todos: state.firebase.ordered.todos })) )