Skip to content

Instantly share code, notes, and snippets.

@koyta
Created February 27, 2019 14:03
Show Gist options
  • Save koyta/6eb99828dec608fd3615994fadebdee9 to your computer and use it in GitHub Desktop.
Save koyta/6eb99828dec608fd3615994fadebdee9 to your computer and use it in GitHub Desktop.
next js redux use
import React from "react";
import { bindActionCreators } from "redux";
import { startClock, addCount, serverRenderClock } from "../store";
import { connect } from "react-redux";
class Counter extends React.Component {
static getInitialProps({ store, isServer }) {
store.dispatch(serverRenderClock(isServer));
store.dispatch(addCount());
return { isServer };
}
componentDidMount() {
this.timer = this.props.startClock();
}
componentWillUnmount() {
clearInterval(this.timer);
}
render() {
return <div>123</div>;
}
}
const mapDispatchToProps = dispatch => {
return {
addCount: bindActionCreators(addCount, dispatch),
startClock: bindActionCreators(startClock, dispatch)
};
};
export default connect(
null,
mapDispatchToProps
)(Counter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment