Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create next.js example for pullstate #20078

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/with-pullstate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
23 changes: 23 additions & 0 deletions examples/with-pullstate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Pullstate example

This example shows how to integrate Pullstate in Next.js.

In the example we are going to display how to toggle dark mode using the store from pullstate.

## Deploy your own

Deploy the example using [Vercel](https://vercel.com):

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-pullstate)

## How to use

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:

```bash
npx create-next-app --example with-pullstate with-pullstate-app
# or
yarn create next-app --example with-pullstate with-pullstate-app
```

Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
29 changes: 29 additions & 0 deletions examples/with-pullstate/components/dark-mode-demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { PullstateCore } from '../stores'

export default function DarkModeDemo() {
const { UIStore } = PullstateCore.useStores()
const isDarkMode = UIStore.useState((s) => s.isDarkMode)

return (
<div
style={{
backgroundColor: isDarkMode ? '#333' : '#CCC',
color: isDarkMode ? '#CCC' : '#333',
}}
>
<h1>Pullstate Demo</h1>

<p>Dark Mode is {isDarkMode ? 'On' : 'Off'}</p>

<button
onClick={() =>
UIStore.update((s) => {
s.isDarkMode = !s.isDarkMode
})
}
>
Toggle Dark Mode
</button>
</div>
Comment on lines +8 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This component should have links to the other pages, that's important to see how all pages work together while the app is running.

)
}
16 changes: 16 additions & 0 deletions examples/with-pullstate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "with-pullstate",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"pullstate": "1.20.4",
"react": "^16.9.0",
"react-dom": "^16.9.0"
},
"license": "MIT"
}
13 changes: 13 additions & 0 deletions examples/with-pullstate/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PullstateProvider } from 'pullstate'

import { useHydrate } from '../stores'

export default function App({ Component, pageProps }) {
const instance = useHydrate(pageProps.snapshot)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's give this another name, like useStore.


return (
<PullstateProvider instance={instance}>
<Component {...pageProps} />
</PullstateProvider>
)
}
5 changes: 5 additions & 0 deletions examples/with-pullstate/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import DarkModeDemo from '../components/dark-mode-demo'

export default function Home() {
return <DarkModeDemo />
}
25 changes: 25 additions & 0 deletions examples/with-pullstate/pages/ssg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import DarkModeDemo from '../components/dark-mode-demo'

export default function SSG() {
return <DarkModeDemo />
}

export async function getStaticProps() {
const instance = PullstateCore.instantiate()

// Example of fetching data from some api
const preferences = await new Promise((resolve) =>
resolve({ isDarkMode: false })
)

// Update the store on the server before page render
instance.stores.UIStore.update((s) => {
s.isDarkMode = preferences.isDarkMode
})

return {
props: {
snapshot: JSON.stringify(instance.getPullstateSnapshot()),
},
}
}
26 changes: 26 additions & 0 deletions examples/with-pullstate/pages/ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import DarkModeDemo from '../components/dark-mode-demo'
import { PullstateCore } from '../stores'

export default function SSR() {
return <DarkModeDemo />
}

export async function getServerSideProps() {
const instance = PullstateCore.instantiate({ ssr: true })

// Example of fetching data from some api
const preferences = await new Promise((resolve) =>
resolve({ isDarkMode: false })
)

// Update the store on the server before page load
instance.stores.UIStore.update((s) => {
s.isDarkMode = preferences.isDarkMode
})

return {
props: {
snapshot: JSON.stringify(instance.getPullstateSnapshot()),
},
}
}
15 changes: 15 additions & 0 deletions examples/with-pullstate/stores/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createPullstateCore } from 'pullstate'
import { useMemo } from 'react'

import { UIStore } from './ui'

export const PullstateCore = createPullstateCore({
UIStore,
})

export function useHydrate(snapshot) {
return useMemo(() => {
if (!snapshot) return PullstateCore.instantiate()
return PullstateCore.instantiate({ hydrateSnapshot: JSON.parse(snapshot) })
}, [snapshot])
}
3 changes: 3 additions & 0 deletions examples/with-pullstate/stores/ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Store } from 'pullstate'

export const UIStore = new Store({ isDarkMode: true })