CSFrequency / react-firebase-hooks

React Hooks for Firebase.
Apache License 2.0
3.58k stars 305 forks source link

Feature: Allow an initial value to be returned by the hooks #190

Closed dylanwatsonsoftware closed 2 years ago

dylanwatsonsoftware commented 2 years ago

In NextJS the idea is that you preload the data at build time. This then gets passed to your component via props.

If I have a component that uses say useCollectionData, it would be useful to specify an initial value that the hook should return whilst the data is loading. As soon as the firebase query has loaded, this value would be ignored and replaced with the value from the firebase query.

It is currently possible to do this outside of the firebase hook but it's a little awkward and this would really simplify the code for this usecase (i.e any nextjs project)

I think it'd be a pretty simple change so happy to put up a PR if you'd be interested in accepting a feature like this.

Thanks, Dylan.

chrisbianca commented 2 years ago

Hi Dylan, this sounds like it would be a very useful feature so am more than happy to review a PR for it. As you say it should be a relatively simple change, but shout if you have any issues and I am happy to take a look myself.

dylanwatsonsoftware commented 2 years ago

Ok awesome. will try to find time this week to take a look and see how far I get!

dylanwatsonsoftware commented 2 years ago

So, I finally got a local copy of react-firebase-hooks hooked up to my local project. For those giving it a go, the main issue was that I need to:

  1. Run npm link in the root of the library directory
  2. Run npm link react-firebase-hooks in the root of my projects directory
  3. The step missing from most documentation was that I then needed to run npm link in the react-firebase-hooks/node_mopdules/react directory to ensure that both projects used the same version of react
  4. Then run npm link react in the root of the project directory
dylanwatsonsoftware commented 2 years ago

NextJS seems to have some issues with Firebase v9 so my plan is to raise the PR against the v3 branch and let you pull it into master later.

chrisbianca commented 2 years ago

Now part of: https://github.com/CSFrequency/react-firebase-hooks/releases/tag/v3.0.5