forked from vercel/next.js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor with redux saga (vercel#13342)
Related to [11014](vercel#11014) Upgraded all the packages from package.json, removed the saga-wrapper package since it is totally outdated to today's use. I have refactored the whole example using the new API of the next-redux-wrapper package, using their new support of "getStaticProps". All of the class components were converted to functional, using the new redux hooks API. If you want me to change anything or you are not satisfied with any given change, I'm open to suggestions.
- Loading branch information
1 parent
72f8c19
commit 8454438
Showing
9 changed files
with
92 additions
and
106 deletions.
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
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 |
---|---|---|
@@ -1,40 +1,26 @@ | ||
import { Component } from 'react' | ||
import { connect } from 'react-redux' | ||
import { useSelector, useDispatch } from 'react-redux' | ||
|
||
import { increment, decrement, reset } from '../actions' | ||
|
||
class Counter extends Component { | ||
increment = () => { | ||
this.props.dispatch(increment()) | ||
} | ||
const Counter = () => { | ||
const count = useSelector((state) => state.count) | ||
const dispatch = useDispatch() | ||
|
||
decrement = () => { | ||
this.props.dispatch(decrement()) | ||
} | ||
|
||
reset = () => { | ||
this.props.dispatch(reset()) | ||
} | ||
|
||
render() { | ||
const { count } = this.props | ||
return ( | ||
<div> | ||
<style jsx>{` | ||
div { | ||
padding: 0 0 20px 0; | ||
} | ||
`}</style> | ||
<h1> | ||
Count: <span>{count}</span> | ||
</h1> | ||
<button onClick={this.increment}>+1</button> | ||
<button onClick={this.decrement}>-1</button> | ||
<button onClick={this.reset}>Reset</button> | ||
</div> | ||
) | ||
} | ||
return ( | ||
<div> | ||
<style jsx>{` | ||
div { | ||
padding: 0 0 20px 0; | ||
} | ||
`}</style> | ||
<h1> | ||
Count: <span>{count}</span> | ||
</h1> | ||
<button onClick={() => dispatch(increment())}>+1</button> | ||
<button onClick={() => dispatch(decrement())}>-1</button> | ||
<button onClick={() => dispatch(reset())}>Reset</button> | ||
</div> | ||
) | ||
} | ||
|
||
const mapStateToProps = ({ count }) => ({ count }) | ||
export default connect(mapStateToProps)(Counter) | ||
export default Counter |
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
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
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 |
---|---|---|
@@ -1,29 +1,27 @@ | ||
import App from 'next/app' | ||
import { Provider } from 'react-redux' | ||
import withRedux from 'next-redux-wrapper' | ||
import withReduxSaga from 'next-redux-saga' | ||
|
||
import createStore from '../store' | ||
import { END } from 'redux-saga' | ||
import { wrapper } from '../store' | ||
|
||
class MyApp extends App { | ||
static async getInitialProps({ Component, ctx }) { | ||
let pageProps = {} | ||
const pageProps = { | ||
...(Component.getInitialProps | ||
? await Component.getInitialProps(ctx) | ||
: {}), | ||
} | ||
|
||
if (Component.getInitialProps) { | ||
pageProps = await Component.getInitialProps({ ctx }) | ||
if (ctx.req) { | ||
ctx.store.dispatch(END) | ||
await ctx.store.sagaTask.toPromise() | ||
} | ||
|
||
return { pageProps } | ||
} | ||
|
||
render() { | ||
const { Component, pageProps, store } = this.props | ||
return ( | ||
<Provider store={store}> | ||
<Component {...pageProps} /> | ||
</Provider> | ||
) | ||
const { Component, pageProps } = this.props | ||
return <Component {...pageProps} /> | ||
} | ||
} | ||
|
||
export default withRedux(createStore)(withReduxSaga(MyApp)) | ||
export default wrapper.withRedux(MyApp) |
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 |
---|---|---|
@@ -1,28 +1,31 @@ | ||
import { Component } from 'react' | ||
import { connect } from 'react-redux' | ||
|
||
import { useEffect } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import { END } from 'redux-saga' | ||
import { wrapper } from '../store' | ||
import { loadData, startClock, tickClock } from '../actions' | ||
import Page from '../components/page' | ||
|
||
class Index extends Component { | ||
static async getInitialProps(props) { | ||
const { store, isServer } = props.ctx | ||
store.dispatch(tickClock(isServer)) | ||
|
||
if (!store.getState().placeholderData) { | ||
store.dispatch(loadData()) | ||
} | ||
const Index = () => { | ||
const dispatch = useDispatch() | ||
|
||
return { isServer } | ||
} | ||
useEffect(() => { | ||
dispatch(startClock()) | ||
}, [dispatch]) | ||
return ( | ||
<> | ||
<Page title="Index Page" linkTo="/other" NavigateTo="Other Page" /> | ||
</> | ||
) | ||
} | ||
|
||
componentDidMount() { | ||
this.props.dispatch(startClock()) | ||
} | ||
export const getStaticProps = wrapper.getStaticProps(async ({ store }) => { | ||
store.dispatch(tickClock(false)) | ||
|
||
render() { | ||
return <Page title="Index Page" linkTo="/other" NavigateTo="Other Page" /> | ||
if (!store.getState().placeholderData) { | ||
store.dispatch(loadData()) | ||
store.dispatch(END) | ||
} | ||
} | ||
await store.sagaTask.toPromise() | ||
}) | ||
|
||
export default connect()(Index) | ||
export default Index |
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 |
---|---|---|
@@ -1,23 +1,20 @@ | ||
import { Component } from 'react' | ||
import { connect } from 'react-redux' | ||
|
||
import { useEffect } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import { wrapper } from '../store' | ||
import { startClock, tickClock } from '../actions' | ||
import Page from '../components/page' | ||
|
||
class Other extends Component { | ||
static async getInitialProps(props) { | ||
const { store, isServer } = props.ctx | ||
store.dispatch(tickClock(isServer)) | ||
return { isServer } | ||
} | ||
|
||
componentDidMount() { | ||
this.props.dispatch(startClock()) | ||
} | ||
const Other = () => { | ||
const dispatch = useDispatch() | ||
useEffect(() => { | ||
dispatch(startClock()) | ||
}, [dispatch]) | ||
|
||
render() { | ||
return <Page title="Other Page" linkTo="/" NavigateTo="Index Page" /> | ||
} | ||
return <Page title="Other Page" linkTo="/" NavigateTo="Index Page" /> | ||
} | ||
|
||
export default connect()(Other) | ||
export const getStaticProps = wrapper.getStaticProps(async ({ store }) => { | ||
store.dispatch(tickClock(false)) | ||
}) | ||
|
||
export default Other |
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
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