firebase / functions-samples

Collection of sample apps showcasing popular use cases using Cloud Functions for Firebase
https://firebase.google.com/docs/functions
Apache License 2.0
12k stars 3.84k forks source link

Example with nextjs doesn't work with React hooks #601

Open Siegrift opened 4 years ago

Siegrift commented 4 years ago

How to reproduce these conditions

I have set up an example repo where you can check the bug.

Steps to set up and reproduce 1) git clone https://github.com/Siegrift/nextjs-firebase-example 2) cd src/app 3) yarn && yarn dev 4) navigate to http://localhost:3000/about You will get: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.

Expected behavior

I don't think that this example is valid anymore. I was trying to figure out how to make nextjs work with firebase, but I couldn't figure it out.... Anyway, the expected behaviour is to make it work with nextjs and newest React (e.g. react hooks).

Actual behavior

Error in application and I am unable to configure nextjs with firebase.

maotora commented 4 years ago

Did you solve this? I'm experiencing the same issue.

Siegrift commented 4 years ago

Unfortunately no, but I didn't needed that much. I use firebase and firestore and I've set it up manually inside the project (https://firebase.google.com/docs/web/setup)

maotora commented 4 years ago

So you are hosting your web app on static hosting? My Nextjs + Antd app got really slow as the size grew bigger so I thought only SSR would lift some rendering load.

I just posted a question on stackoverflow referencing this issue and a fork of your reproducible bug repo.

Siegrift commented 4 years ago

Yes, I am using zeit hosting (default for nextjs) and I configured firebase manually. This was the best I could do in a few days and I didn't want to spend much more on it

On Sat, 19 Oct 2019, 16:42 Maotora ᕙ(⇀‸↼‶)ᕗ, notifications@github.com wrote:

So you are hosting your web app on static hosting? My Nextjs + Antd app got really slow as the size grew bigger so I thought only SSR would lift some rendering load.

I just posted a question on stackoverflow https://stackoverflow.com/questions/58464758/firebase-cloud-functions-wont-work-with-react-hooks-on-nextjs-ssr referencing this issue and a fork of your reproducible bug repo.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/firebase/functions-samples/issues/601?email_source=notifications&email_token=AFNA44RMHW7WFVINL7FQMJ3QPMMEHA5CNFSM4IK34NF2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBXR6EY#issuecomment-544153363, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNA44UN4CSZAS447HQI3CLQPMMEHANCNFSM4IK34NFQ .

RahmanQureshi commented 4 years ago

The problem superficially stems from two copies of the react source code. I'm not 100% sure how it works so I won't even try to explain the root cause of the problem.

To fix the problem, please place the app inside the functions folder and remove package.json. Unfortunately, all the dependencies of app must necessarily be dependencies of functions. Then, change next.config.js to this:

'use strict';

module.exports = { distDir: './next' };

and index.js to this:

const dir = __dirname + "/app" const app = next({ dev, dir, conf: {distDir: 'next'} })

jthegedus commented 4 years ago

Hi, I wrote the initial example. This example hasn't been kept up to date as I was maintaining my own example and the example in the nextjs repo. My most up to date example does function with React hooks if you would like to check that out https://github.com/jthegedus/firebase-gcp-examples/tree/master/functions-nextjs