-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's give this another name, like |
||
|
||
return ( | ||
<PullstateProvider instance={instance}> | ||
<Component {...pageProps} /> | ||
</PullstateProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.