Code Monkey home page Code Monkey logo

Comments (8)

rauchg avatar rauchg commented on April 28, 2024 1

@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

image

I wrote up a quick example here: http://jsbin.com/qezacugofo/

  1. let's show that the server-rendered version has a slight different (darker) green
  2. let's show than when it updates on the client side, it becomes a brighter green
  3. 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.

longseespace avatar longseespace commented on April 28, 2024 1

@impronunciable The wiki entry seems to be removed. Can you help? https://github.com/zeit/next.js/wiki/Redux-example

from next.js.

rauchg avatar rauchg commented on April 28, 2024

Note: If it becomes too long we can link to a wiki page instead

from next.js.

impronunciable avatar impronunciable commented on April 28, 2024

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.

impronunciable avatar impronunciable commented on April 28, 2024

Added as https://github.com/zeit/next.js/wiki/Redux-example

from next.js.

impronunciable avatar impronunciable commented on April 28, 2024

updated in wiki

from next.js.

rauchg avatar rauchg commented on April 28, 2024

It's now on the examples folder :D We should probably re-create the page with a link to the examples

from next.js.

rauchg avatar rauchg commented on April 28, 2024

Done

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.