forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
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 thunk example (vercel#13336)
Related to [11014](vercel#11014) 1. I have changed the component from class to functional. 2. I have removed the getInitialProps and used getStaticProps instead. 3. I have removed the redundant connect to redux @ the index page, due to the fact that now we can dispatch the action with the required hook. 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
8454438
commit 9d2cfb5
Showing
3 changed files
with
27 additions
and
32 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
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,38 +1,33 @@ | ||
import { PureComponent } from 'react' | ||
import { connect } from 'react-redux' | ||
import { useEffect } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import Link from 'next/link' | ||
import getStore from '../store' | ||
import { startClock, serverRenderClock } from '../actions' | ||
import Examples from '../components/examples' | ||
|
||
class Index extends PureComponent { | ||
static getInitialProps({ store, req }) { | ||
store.dispatch(serverRenderClock(!!req)) | ||
const Index = () => { | ||
const dispatch = useDispatch() | ||
useEffect(() => { | ||
dispatch(startClock()) | ||
}, [dispatch]) | ||
|
||
return {} | ||
} | ||
return ( | ||
<> | ||
<Examples /> | ||
<Link href="/show-redux-state"> | ||
<a>Click to see current Redux State</a> | ||
</Link> | ||
</> | ||
) | ||
} | ||
|
||
componentDidMount() { | ||
this.timer = this.props.startClock() | ||
} | ||
export async function getStaticProps() { | ||
const store = getStore() | ||
store.dispatch(serverRenderClock()) | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.timer) | ||
return { | ||
props: {}, | ||
} | ||
|
||
render() { | ||
return ( | ||
<> | ||
<Examples /> | ||
<Link href="/show-redux-state"> | ||
<a>Click to see current Redux State</a> | ||
</Link> | ||
</> | ||
) | ||
} | ||
} | ||
|
||
const mapDispatchToProps = { | ||
startClock, | ||
} | ||
|
||
export default connect(null, mapDispatchToProps)(Index) | ||
export default Index |