How to properly work with promises, Firebase and React's UseEffect hook?

Asked
Active3 hr before
Viewed126 times

7 Answers

firebasepromisesproperlyreact
90%

Basically what I'm trying to achieve is to run some code only after a variable is not undefined anymore. However, I'm feeling very confused with the use of promises to await firebase responses, as well as with the use of React's useEffect hook.,1) First, it will execute all the thing which is outside the useEffect and it's dependency whenever there is change in state, so keep that in mind, Short story about someone who is exiled on a prison planet where some people bury themselves in the ground or grow extra limbs

load more v
88%

If you haven’t tried the app out yet, you might be wondering how it works. It allows you to create a new grocery list. The grocery list’s URL can be shared with other users, who can then join the list and add their own grocery items to the list.,Next, let’s look at how grocery list data is stored in React component state.,For example, the following code creates a new grocery list document in the groceryLists collection:

Pretag
 Pretag team - issue, fix, solve, resolve
72%

And that’s why the compiler is yielding in Typescript. This pattern is also not working in plain JS as React is not waiting for a promise.,By using the following technique, we will be able to use async function in our effects :,Let’s see why this error appears by taking the definition of what an async function is:

👾At first glance, you could have the idea to do something similar to get your content from a remote API as an example.

const MyFunctionnalComponent: React.FC = (props) => {  useEffect(async () => {   await loadContent();  }, []);  return <div></div>;}
load more v
65%

Using the Context API to make user data available through our application,Make our user state available via the Context API,React in our application accordingly to changes in the user state

Pretag
 Pretag team - issue, fix, solve, resolve
75%

This tutorial demonstrates the use of hooks in your react application to better integrate firebase authentication and firestore data fetching. Before starting, it’s helpful to have a basic understanding of hooks, firebase authentication and firestore. By the end, we will be building some of the hooks found in our example application, Julienne.app.,Finally, let’s create a hook that monitors the firebase authentication state. This hook will create state which uses a useState callback to determine whether a user session already exists. The callback is a useful way to initialize state with a value only upon the first mount of a componment.,If you plan to use hooks with firebase throughout your application, I recommend checking out react-firebase-hooks. It provides some useful helpers that allows us to reuse some of the logic that we wrote above.

const userContext = React.createContext({
   user: null,
})
load more v
40%

Learn the basics of asynchronous functions and promises by fetching data from an API using fetch, useEffect and useState,Concurrency, swipe actions, search feature, AttributedStrings and accessibility were concepts discussed at WWDC21. This course explores all these topics, in addition to data hosting in Contentful and data fetching using Apollo GraphQL,Setting up a database in an application can be quite complex, as you need to create a server, manage it and make sure that it stays secure. Luckily, if you have Firebase set up in your React application, you can easily set up Firestore to act as your database. It's serverless, meaning that you don't need to worry about managing the server as it is already done by Google. Let's learn how to add it to our React application and read and write from Firestore!

Pretag
 Pretag team - issue, fix, solve, resolve
22%

Hooks are a new addition in React that lets you use state and other React features without writing a class. This website provides easy to understand code examples to help you learn how hooks work and inspire you to take advantage of them in your next project.,react-async - React component and hook for declarative promise resolution and data fetching.,donavon/use-dark-mode - A more configurable implementation of this hook that syncs changes across browser tabs and handles SSR. Provided much of the code and inspiration for this post.

Basically, what this hook does is that, it takes a parameter with value true or false and toggles that value to opposite. It's useful when we want to take some action into it's opposite action, for example: show and hide modal, show more/show less text, open/close side menu.

import { useCallback, useState } from 'react';


// Usage
function App() {
    // Call the hook which returns, current value and the toggler function
    const [isTextChanged, setIsTextChanged] = useToggle();
    
    return (
        <button onClick={setIsTextChanged}>{isTextChanged ? 'Toggled' : 'Click to Toggle'}</button>
    );
}

// Hook
// Parameter is the boolean, with default "false" value
const useToggle = (initialState = false) => {
    // Initialize the state
    const [state, setState] = useState(initialState);
    
    // Define and memorize toggler function in case we pass down the comopnent,
    // This function change the boolean value to it's opposite value
    const toggle = useCallback(() => setState(state => !state), []);
    
    return [state, toggle]
}
import { useCallback, useState } from 'react';


// Usage
function App() {
    // Call the hook which returns, current value and the toggler function
    const [isTextChanged, setIsTextChanged] = useToggle();

    return (
        <button onClick={setIsTextChanged}>{isTextChanged ? 'Toggled' : 'Click to Toggle'}</button>
    );
}

// Hook
// Parameter is the boolean, with default "false" value
const useToggle = (initialState: boolean = false): [boolean, any] => {
    // Initialize the state
    const [state, setState] = useState<boolean>(initialState);

    // Define and memorize toggler function in case we pass down the comopnent,
    // This function change the boolean value to it's opposite value
    const toggle = useCallback((): void => setState(state => !state), []);

    return [state, toggle]
}
load more v

Other "firebase-promises" queries related to "How to properly work with promises, Firebase and React's UseEffect hook?"