Skip to content

Commit b8d7abe

Browse files
committedNov 5, 2019
Improved nested Hoc with connected component example #5
1 parent c9f2566 commit b8d7abe

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ export const withConnectedCount = <BaseProps extends InjectedProps>(
753753

754754
const ConnectedHoc = connect<
755755
ReturnType<typeof mapStateToProps>,
756-
typeof dispatchProps,
756+
typeof dispatchProps, // use "undefined" if NOT using dispatchProps
757757
HocProps,
758758
RootState
759759
>(

‎playground/src/hoc/with-connected-count.tsx

+11-10
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ interface InjectedProps {
1313
export const withConnectedCount = <BaseProps extends InjectedProps>(
1414
BaseComponent: React.ComponentType<BaseProps>
1515
) => {
16-
type HocProps = Diff<BaseProps, InjectedProps> & {
17-
// here you can extend hoc with new props
18-
initialCount?: number;
19-
};
20-
2116
const mapStateToProps = (state: RootState) => ({
2217
count: countersSelectors.getReduxCounter(state.counters),
2318
});
@@ -26,18 +21,24 @@ export const withConnectedCount = <BaseProps extends InjectedProps>(
2621
onIncrement: countersActions.increment,
2722
};
2823

29-
class Hoc extends React.Component<InjectedProps> {
24+
type HocProps = ReturnType<typeof mapStateToProps> &
25+
typeof dispatchProps & {
26+
// here you can extend ConnectedHoc with new props
27+
overrideCount?: number;
28+
};
29+
30+
class Hoc extends React.Component<HocProps> {
3031
// Enhance component name for debugging and React-Dev-Tools
3132
static displayName = `withConnectedCount(${BaseComponent.name})`;
3233
// reference to original wrapped component
3334
static readonly WrappedComponent = BaseComponent;
3435

3536
render() {
36-
const { count, onIncrement, ...restProps } = this.props;
37+
const { count, onIncrement, overrideCount, ...restProps } = this.props;
3738

3839
return (
3940
<BaseComponent
40-
count={count} // injected
41+
count={overrideCount || count} // injected
4142
onIncrement={onIncrement} // injected
4243
{...(restProps as BaseProps)}
4344
/>
@@ -47,8 +48,8 @@ export const withConnectedCount = <BaseProps extends InjectedProps>(
4748

4849
const ConnectedHoc = connect<
4950
ReturnType<typeof mapStateToProps>,
50-
typeof dispatchProps,
51-
HocProps,
51+
typeof dispatchProps, // use "undefined" if NOT using dispatchProps
52+
Diff<BaseProps, InjectedProps>,
5253
RootState
5354
>(
5455
mapStateToProps,

‎playground/src/hoc/with-connected-count.usage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import { FCCounter } from '../components';
66
const FCCounterWithConnectedCount = withConnectedCount(FCCounter);
77

88
export default () => (
9-
<FCCounterWithConnectedCount initialCount={5} label={'FCCounterWithState'} />
9+
<FCCounterWithConnectedCount overrideCount={5} label={'FCCounterWithState'} />
1010
);

0 commit comments

Comments
 (0)
Failed to load comments.