Comments (8)
@impronunciable some feedback:
- Maybe we have an action
INIT
that dispatches the update in this case (with thunk)? Not necessary, but might be more clear for everyone - Needs more wording. Walk through the steps, what
getInitialProps
does, how it interacts with the store, etc. - Needs an example of an async action. Even a
setTimeout
would do for this case
I think we also need to frame the example a bit better in the beginning. For example:
let's write a clock that server-renders and then updates every second! The time will be stored as global Redux state
I wrote up a quick example here: http://jsbin.com/qezacugofo/
- let's show that the server-rendered version has a slight different (darker) green
- let's show than when it updates on the client side, it becomes a brighter green
- let's include a little gif of how pressing refresh works by server-rendering, and after a delay it becomes a brighter green
from next.js.
@impronunciable The wiki entry seems to be removed. Can you help? https://github.com/zeit/next.js/wiki/Redux-example
from next.js.
Note: If it becomes too long we can link to a wiki page instead
from next.js.
This is what I came up with after a long trip with no rest (I had a nicer example including graphql, the giphy api and kittens but was more complicated)
// components/store.js
➜ aver cat components/store.js
import { createStore } from 'redux'
export const reducer = (state = { lastUpdate: 0 }, action) => {
switch (action.type) {
case 'UPDATE': return { lastUpdate: Date.now() }
default: return state
}
}
export const initStore = (reducer, initialState, isServer) => {
if (isServer && typeof window === 'undefined') {
return createStore(reducer, initialState)
} else if (window.store) {
return window.store
} else {
window.store = createStore(reducer, initialState)
return window.store
}
}
// pages/index.js
import React from 'react'
import { connect, Provider } from 'react-redux'
import { reducer, initStore } from '../components/store'
import Link from 'next/link'
export default class Index extends React.Component {
static getInitialProps ({ req }) {
const isServer = !!req
const store = initStore(reducer, null, isServer)
store.dispatch({type: 'UPDATE'})
return { initialState: store.getState(), isServer }
}
constructor (props) {
super(props)
this.store = initStore(reducer, props.initialState, props.isServer)
}
render () {
return (
<Provider store={this.store}>
<MyData />
</Provider>
)
}
}
const MyData = connect(state => state)(({ lastUpdate }) => (
<div>
<p>{lastUpdate}</p>
<Link href='/viewer'>See the loaded state</Link>
</div>
))
// pages/viewer.js
import React from 'react'
import { connect, Provider } from 'react-redux'
import { reducer, initStore } from '../components/store'
import Link from 'next/link'
export default class Viewer extends React.Component {
static getInitialProps ({ req }) {
const isServer = !!req
const store = initStore(reducer, null, isServer)
return { initialState: store.getState(), isServer }
}
constructor (props) {
super(props)
this.store = initStore(reducer, props.initialState, props.isServer)
}
render () {
return (
<Provider store={this.store}>
<MoarData />
</Provider>
)
}
}
const MoarData = connect(state => state)(({ lastUpdate }) => (
<div>
<p>this was the {lastUpdate}</p>
</div>
))
from next.js.
Added as https://github.com/zeit/next.js/wiki/Redux-example
from next.js.
updated in wiki
from next.js.
It's now on the examples
folder :D We should probably re-create the page with a link to the examples
from next.js.
Done
from next.js.
Related Issues (20)
- Discrepancy in searchParams between server and client after clicking `Link` HOT 1
- Dev server is taking too much memory in my Macbook & Windows machine HOT 1
- Dev server is taking too much memory in my Macbook & Windows machine HOT 3
- 创建新的next14项目中,产生了一个错误 HOT 1
- 创建hello word,出现未知错误 HOT 1
- 构建项目出现问题 HOT 4
- npm run build element type is invalid expected a string (for built-in components) or a class/function HOT 1
- npm run build element type is invalid expected a string (for built-in components) or a class/function HOT 1
- npm run build element type is invalid expected a string (for built-in components) or a class/function
- Docs: Allow https://nextjs.org/ to work when PerformanceObserver is undefined
- Memory Leak with Next.js's global fetch. Tested against http module HOT 18
- Missing styles on not-found (404) page HOT 1
- > Build error occurred HOT 1
- router.push from `next/navigation` does not work in a client component HOT 1
- Error: Collecting page data for undefined is still timing out after 2 attempts. See more info here https://nextjs.org/docs/messages/page-data-collection-timeout -- 21:32:45.793 | at onRestart #64216 HOT 2
- __NEXT_DATA__ JSON Data Displayed Instead of UI (Pages Router)
- State of `issuerLayer=="rsc"` for the library authors
- Cannot set property attribute on metadata using generateMetadata API
- Root Route Intercepting (...) doesn't work when nested under a dynamic route segment
- PPR + Route Interception leads to corrupted params value
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next.js.