question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NextJS with easy-peasy example

See original GitHub issue

Hi folks,

First of all: thank you so much for this awesome library! 🥇

I’m trying to use EP in a new project where I started using NextJS. I made some research and found interesting post on this github (mostly the issue #171) and on the web, but it seems all the examples I could find were outdated (compared to the current nextjs and redux-wrapper versions & guidelines).

I started from this repo: https://github.com/vercel/next.js/tree/canary/examples/with-redux-wrapper

I simplified the examples and integrated EP to end up with: https://codesandbox.io/s/next-redux-wrapper-easy-peasy-98nr6?file=/store/store.js

I would really appreciate if you (@ctrlplusb) or any skillful person over here could review this sandbox and give some feedbacks, especially on the following files and their comments:

// store/store.js

import { createStore, action, actionOn } from 'easy-peasy'
import { HYDRATE, createWrapper } from 'next-redux-wrapper'

const model = {
    count: 0,
    addCount: action((state) => {
        state.count += 1
    }),
    initCount: action((state, init) => {
        state.count = init
    }),
    // 👇 catch the HYDRATE actions (__NEXT_REDUX_WRAPPER_HYDRATE__)
    //
    // How would you do if you have a nested model ?
    // e.g.: const model = { counter: { count: 0, addCount: ... }, user: { name, getName: ... }) }
    //
    // TODO: client side state reconciliation during hydration
    // --> https://github.com/kirill-konshin/next-redux-wrapper#state-reconciliation-during-hydration
    ssrHydrate: actionOn(
        () => HYDRATE,
        (state, target) => {
            state.count = target.payload.count
        }
    )
}

const initStore = () => { return createStore(model) }

export const wrapper = createWrapper(initStore)
// pages/_app.js

import { wrapper } from '../store/store'
import { useStore } from 'react-redux'
import { StoreProvider } from 'easy-peasy'

const WrappedApp = ({ Component, pageProps }) => {
    const store = useStore()

    return (
        <StoreProvider store={store}>
            <Component {...pageProps} />
        </StoreProvider>
    )
}

export default wrapper.withRedux(WrappedApp)
// pages/other.js

import Page from '../components/Page'
import { wrapper } from '../store/store'

const Other = (props) => {
    return <Page title="Other Page" linkTo="/" />
}

// 👇 simulate data fetching to hydrate store
export const getServerSideProps = wrapper.getServerSideProps(
    ({ store }) => {
        console.log('ssr init count to 8')
        // 👇 is there a way to dispatch EP-style ? (eg: actions.initCount(8))
        store.dispatch({ type: '@action.initCount', payload: 8 })
    }
)

export default Other

As I don’t fully grasp all the things happening behind the scene, I want to make sure I’m not going crazy and wrong stuff ;D

On top of that, it could be the start of a nice example to help other people. I will certainly make a TS version when everything is validated 😃

Cheers !

Benjamin

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
binajmencommented, Mar 3, 2021

This is very strange… The buttons are in fact working. If you

  1. Visit /ssr
  2. Click on the Add to basket button n times for a product
  3. Visit /ssr again
  4. Click on the Add to basket button m times for the same product
  5. Refresh the page OR Visit /index

You will notice that your basket has n + m products. I can’t find why this is happening…

@ctrlplusb You are probably busy on other stuffs, but perhaps your knowledge of Easy-Peasy and Redux in general could point out the issue?

3reactions
ctrlplusbcommented, Feb 24, 2021

Great to see this evolving @binajmen. I am in the middle of a few other things EP related, but this is on my radar. 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

nextjs-with-easy-peasy - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >
Developers - NextJS with easy-peasy example - - Bountysource
I'm trying to use EP in a new project where I started using NextJS. I made some research and found interesting post on...
Read more >
Why when following the quick start for easy peasy is my state ...
I was trying to use easy peasy for global state management within a nextjs app and was running into problems where the state...
Read more >
Easy Peasy v5
Vegetarian friendly state for React ... Easy Peasy is an abstraction of Redux, providing a reimagined API that focuses on developer experience. It ......
Read more >
Next.js apps on SiteHost Cloud Containers: easy peasy
Container ports configuration After connecting to the Container via SSH, we remove the example files and install our app on the /container/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found