functions-samples icon indicating copy to clipboard operation
functions-samples copied to clipboard

Example with nextjs doesn't work with React hooks

Open Siegrift opened this issue 6 years ago • 6 comments

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.

Siegrift avatar Aug 11 '19 14:08 Siegrift

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

maotora avatar Oct 19 '19 13:10 maotora

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)

Siegrift avatar Oct 19 '19 14:10 Siegrift

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.

maotora avatar Oct 19 '19 14:10 maotora

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 ᕙ(⇀‸↼‶)ᕗ, [email protected] 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 .

Siegrift avatar Oct 19 '19 15:10 Siegrift

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'} })

RahmanQureshi avatar May 18 '20 03:05 RahmanQureshi

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

jthegedus avatar May 18 '20 04:05 jthegedus