next redux saga
ver side rendering (SSR)
因为next.js已经大量增长,其他具有更好支持的软件包涵盖了redux-saga SSR功能。有关更多信息,请参见#79。
redux-sagahoc for next.js。服务器端渲染的控制的redux-saga执行。
注意:自4.0.0版以来,不再支持同步HOC!
yarn add next-redux-saga查看官方的下一步。JS示例或克隆此存储库并运行本地示例。
yarnyarn startnext-redux-saga使用Next-Redux-Wrapper创建的Redux Store。请参阅他们的文档以获取更多信息。
import { applyMiddleware , createStore } from 'redux'
import createSagaMiddleware from 'redux-saga'
import { createWrapper } from 'next-redux-wrapper'
import rootReducer from './root-reducer'
import rootSaga from './root-saga'
const makeStore = context => {
const sagaMiddleware = createSagaMiddleware ( )
const store = createStore (
rootReducer ,
applyMiddleware ( sagaMiddleware ) ,
)
store . sagaTask = sagaMiddleware . run ( rootSaga )
return store
}
const wrapper = createWrapper ( makeStore )
export default wrapper_app.js组件 import React from 'react'
import App from 'next/app'
import withReduxSaga from 'next-redux-saga'
import wrapper from './store-wrapper'
class ExampleApp extends App {
static async getInitialProps ( { Component , ctx } ) {
let pageProps = { }
if ( Component . getInitialProps ) {
pageProps = await Component . getInitialProps ( ctx )
}
return { pageProps }
}
render ( ) {
const { Component , pageProps } = this . props
return (
< Component { ... pageProps } />
)
}
}
export default wrapper . withRedux ( withReduxSaga ( ExampleApp ) ) import React , { Component } from 'react'
import { connect } from 'react-redux'
class ExamplePage extends Component {
static async getInitialProps ( { store } ) {
store . dispatch ( { type : 'SOME_ASYNC_ACTION_REQUEST' } )
return { staticData : 'Hello world!' }
}
render ( ) {
return < div > { this . props . staticData } </ div >
}
}
export default connect ( state => state ) ( ExamplePage ) 布伦特粉 | 蒂蒙·博特(Timon Borter) | Artem Abzanov | 罗宾·哈伯姆(Robbin Habermehl) |
yarnyarn linkyarn test --watch并开始进行更改yarn link next-redux-saga来测试您在实际项目中的更改该项目是根据MIT许可条款获得许可的。有关更多信息,请参见许可证文件。