Skip to content
This repository was archived by the owner on Feb 16, 2021. It is now read-only.

Commit 99f5f5a

Browse files
OliverJAshgcanti
authored andcommitted
Update withDefaults example to omit undefined input prop values
Fixes #20
1 parent 9e4c5b5 commit 99f5f5a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

README.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,19 @@ console.log(v2.zip(v1.append(v1))) // Vector([[2,1],[3,1]])
6868

6969
```ts
7070
import * as React from 'react'
71-
import { ObjectDiff } from 'typelevel-ts'
71+
import { ObjectDiff } from 'typelevel-ts';
72+
import isUndefined from 'lodash/isUndefined';
73+
import omitBy from 'lodash/omitBy';
7274

7375
function withDefaults<D, A extends D>(C: React.ComponentType<A>, defaults: D): React.SFC<ObjectDiff<A, D>> {
74-
return props => <C {...defaults} {...props} />
75-
}
76+
return props => {
77+
// If a prop is provided with a value of `undefined`, we want the default prop to take
78+
// precendence. This ensures the behaviour matches that of React's built-in `defaultProps`
79+
// static property on components.
80+
const propsMinusUndefined = omitBy(props, isUndefined);
81+
return <C {...defaults} {...propsMinusUndefined} />;
82+
};
83+
};
7684

7785
class Foo extends React.Component<{ bar: string; baz: number }> {}
7886
const DefaultedFoo = withDefaults(Foo, { baz: 1 })

0 commit comments

Comments
 (0)