import * as React from 'react'; import { Dispatch } from 'redux'; import { connect } from 'react-redux'; import { connectWithLifecycle, LifecycleDispatchProps, applyLifecycle, LifecycleStateProps, LifecycleComponent, } from 'react-lifecycle-component'; interface State { stateFoo: number; } interface Props extends StateProps, DispatchProps { } interface StateProps { propsFoo: number; } interface DispatchProps { bar(): void; } class ComponentFoo extends React.Component { constructor(props: Props) { super(props); this.state = { stateFoo: 0, }; } render() { return (
Props Foo: {this.props.propsFoo}
State Foo: {this.state.stateFoo}
); } } type MapStateProps = StateProps & LifecycleStateProps; function mapStateToProps(): MapStateProps { return { component: ComponentFoo, propsFoo: 8675309, }; } type MapDispatchProps = DispatchProps & LifecycleDispatchProps; function mapDispatchToProps(dispatch: Dispatch): MapDispatchProps { return { bar() { dispatch({ type: 'Bar'}); }, componentWillMount() { dispatch({ type: 'ComponentWillMount'}); }, componentDidMount() { dispatch({ type: 'ComponentDidMount'}); }, componentWillUpdate(nextProps, nextState, nextContext) { const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo; const hasNextContext: boolean = !!nextContext; dispatch({ type: 'ComponentWillUpdate'}); }, componentDidUpdate(nextProps, nextState, nextContext) { const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo; const hasNextContext: boolean = !!nextContext; dispatch({ type: 'ComponentDidUpdate'}); }, componentWillReceiveProps(nextProps, nextContext) { const fooIsGreaterThanZero: boolean = nextProps.propsFoo > 0; const hasNextContext: boolean = !!nextContext; dispatch({ type: 'ComponentWillReceiveProps'}); }, componentWillUnmount() { dispatch({ type: 'ComponentWillUnmount'}); }, shouldComponentUpdate(nextProps, nextState, nextContext) { const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo; const hasNextContext: boolean = !!nextContext; return !fooIsEqual; }, }; } const connectWithLifecylceContainer = connectWithLifecycle(mapStateToProps, mapDispatchToProps)(ComponentFoo); const applyLifecycleContainer = connect(mapStateToProps, mapDispatchToProps)(applyLifecycle(ComponentFoo)); const lifecycleContainer = connect(mapStateToProps, mapDispatchToProps)(LifecycleComponent);