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許可條款獲得許可的。有關更多信息,請參見許可證文件。