pullstate icon indicating copy to clipboard operation
pullstate copied to clipboard

Example with Next.js SSR?

Open tyteen4a03 opened this issue 4 years ago • 15 comments

Is there an example of using this package with Next.js's SSR?

tyteen4a03 avatar Jun 11 '20 22:06 tyteen4a03

Hi @tyteen4a03 . I'm happy to put one together. Is there like a Next.js playground somewhere I can put it on? Or should I just make a new github project example?

lostpebble avatar Jun 12 '20 10:06 lostpebble

Next.js has a sample blog app: https://nextjs.org/learn/basics/create-nextjs-app

tyteen4a03 avatar Jun 12 '20 12:06 tyteen4a03

I've been trying to put together an example today - but Next.js SSR is not the easiest thing to do server customization stuff on.

You need to have some control over the server-side of things, to create the initial store provider and then be able to initialize whatever state you want per route. It seems Next.js allows you to run a getInitialProps on the App, and on each page route - but I don't see any way to actually pass the instance down, so we can't initialize custom state per route.

If you don't have any state to initialize on the server though, then of course its no problem and you can just make use of Pullstate client-side only. But I assume you want to do some per-page state initialization? Or want to make use of Async Actions and resolve the data fetching on the server before rendering?

I might have to look into creating a custom withPullstate() method that you can wrap each page with, but its not ideal. I would be much nicer if they just allowed you to pass state around inside the different getServerSideProps methods, starting from the parent App component.

lostpebble avatar Jun 16 '20 12:06 lostpebble

Pullstate looks really great, congrats :)

Has anyone integrated Next.js and Pullstate?

gusaiani avatar Oct 08 '20 20:10 gusaiani

Thanks @gusaiani :)

I'll try and take another shot at this soon, seeing how Next.js seems to be rather popular these days. Would be nice to have an easy solution for it.

lostpebble avatar Oct 09 '20 10:10 lostpebble

Thank you @lostpebble.

We'll be very inclined to adopting Pullstate at http://www.getcircuit.com with Next!

gusaiani avatar Oct 09 '20 16:10 gusaiani

I'm using Pullstate with Next.js and it works great, except for Fast Refresh. After a fast refresh the store won't update. Trying to figure out what's going on but I posted a small repro over on a Next.js issue: https://github.com/vercel/next.js/issues/13268#issuecomment-709461884

mlynch avatar Oct 15 '20 17:10 mlynch

Hi @mlynch - I've released 1.19.1 with some fixes to make the state selection compatible with the new "concurrent" and StrictMode React stuff. Please check it out, hopefully it solves the Fast Refresh issues.

lostpebble avatar Oct 16 '20 12:10 lostpebble

@lostpebble you legend! Works great, did not expect to wake up and see this. I love pullstate, thanks so much!

mlynch avatar Oct 16 '20 13:10 mlynch

Hahah awesome! Glad to hear its all working, and cheers for the kind words! :)

Appreciate your share on Twitter the other day too - it really pumped up the exposure!

lostpebble avatar Oct 16 '20 14:10 lostpebble

Of course! I expect a lot more people to be using this in time. Also not sure if you saw my dev.to article, let me know if I got anything wrong: https://dev.to/ionic/pullstate-simple-hooks-based-state-management-for-react-5bc4

mlynch avatar Oct 16 '20 15:10 mlynch

Oh jeeze, can't believe I forgot about the article! Yea, loved it- really nice and concise introduction in a great format. Also nice that you went into some of the gotchas of the mutations, something actually overlooked in the pullstate docs and should be mentioned. Really appreciate you putting it together, and on a personal level its a great feeling to see someone write about your project (especially someone who knows their way around this industry)- so thanks for that :)

lostpebble avatar Oct 16 '20 20:10 lostpebble

Hi guys, I'm trying to make pullstate (love the library!) work with Next.js, here's the example PR: https://github.com/vercel/next.js/pull/20078

The only issue is the warning that I'm getting in the console: Pullstate: createPullstate() - Should not be creating the core Pullstate class more than once! In order to re-use pull state, you need to call instantiate() on your already created object.

I'm not sure how to go about solving it. Any ideas?

xeoneux avatar Dec 11 '20 00:12 xeoneux

Hi @xeoneux , and thanks glad you're enjoying the library :)

Also, thanks for trying to get an example put together with Next.js!

Are you still having the issues? I tried your PR example in the Next.js repo- and all seemed fine, except for a missing import of:

import { PullstateCore } from '../stores'

in ssg.js

After that, it built and ran without any issue. The dev version worked first time without issue as well. Didn't notice any error messages in the console.

Let me know if this is still an issue and I'll dive into it deeper.

lostpebble avatar Dec 14 '20 11:12 lostpebble

Hi guys, I'm trying to make pullstate (love the library!) work with Next.js, here's the example PR: vercel/next.js#20078

The only issue is the warning that I'm getting in the console: Pullstate: createPullstate() - Should not be creating the core Pullstate class more than once! In order to re-use pull state, you need to call instantiate() on your already created object.

I'm not sure how to go about solving it. Any ideas?

I'm getting this same issue as well . Would anyone being willing to take a look at my code and help me figure out why this warning keeps popping up? I'm using SSR

rcronin avatar May 13 '21 18:05 rcronin