Use any lodash function as a React component
import react from 'react'
array && array.length ? (
<ul>
{array.map(i => (
<li key={i}>{i}</li>
))}
</ul>
) : (
'Empty list'
)
The example below uses lodash _.isEmpty and _.map as components.
import react from 'react'
import { IsEmpty, Map } from "react-lodash"
<IsEmpty
value={array}
yes="Empty list"
no={() => (
<ul>
<Map collection={array} iteratee={i => <li key={i}>{i}</li>} />
</ul>
)}
/>
You can play with react-lodash
on CodeSandbox
npm install react-lodash
Why?
I wanted to know how things could be rewritten with lodash
as components and if generating them directly from lodash
JSDoc was possible.
The answer to the latter is obviously yes (otherwise, this repo wouldn't exist π). react-lodash
is therefore a 1:1 mapping with lodash
API and all components are generated using npm run generate
.
It also means that not all react-lodash
components will make sense in a React app. Particularly the ones that mutate data.
Does it work?
Yes, you can try it on CodeSandbox.
Should you use it?
If you have a personal/small project and want to play with react-lodash
, feel free. Some components might be useful or provide some interesting features.
For bigger projects, you should probably stick to plain JS as it's more familiar and works better with typing systems.
In any case, I had fun building this project and I hope you'll find the idea entertaining :)
react-lodash uses lodash documentation for prop names.
For example, let's say you want to use _.get
. Based on lodash documentation, it takes an object
and path
arguments, so <Get />
will have the same props.
const object = {
a: {
b: { 1 }
}
}
const path = 'a.b'
// lodash
_.get(object, path)
// react-lodash
<Get object={object} path={path} />
Also every react-lodash component accepts a children
render prop:
<Get object={object} path={path}>
{value => <UpperCase string={value} />}
</Get>
For lodash functions that return a boolean, react-lodash components accept yes
and no
render props:
<IsEmpty
value={array}
yes={() => <p>empty</p>}
no={() => <p>not empty</p>}
/>
You can either use named imports or individually import components
import { IsEmpty } from 'react-lodash'
import IsEmpty from 'react-lodash/lib/IsEmpty'
Below you'll find the 296 available components. For detailed documentation, you can visit https://lodash.com/docs
Note: Since react-lodash
is 1:1 mapping of lodash
, maybe not all components will be relevant in a React application. But at least, you have many options ;)
<Chunk array={} size={} />
β _.chunk<Compact array={} />
β _.compact<Concat array={} values={} />
β _.concat<Difference array={} values={} />
β _.difference<DifferenceBy array={} values={} iteratee={} />
β _.differenceBy<DifferenceWith array={} values={} comparator={} />
β _.differenceWith<Drop array={} n={} />
β _.drop<DropRight array={} n={} />
β _.dropRight<DropRightWhile array={} predicate={} />
β _.dropRightWhile<DropWhile array={} predicate={} />
β _.dropWhile<Fill array={} value={} start={} end={} />
β _.fill<FindIndex array={} predicate={} fromIndex={} />
β _.findIndex<FindLastIndex array={} predicate={} fromIndex={} />
β _.findLastIndex<First array={} />
β _.first<Flatten array={} />
β _.flatten<FlattenDeep array={} />
β _.flattenDeep<FlattenDepth array={} depth={} />
β _.flattenDepth<FromPairs pairs={} />
β _.fromPairs<IndexOf array={} value={} fromIndex={} />
β _.indexOf<Initial array={} />
β _.initial<Intersection arrays={} />
β _.intersection<IntersectionBy arrays={} iteratee={} />
β _.intersectionBy<IntersectionWith arrays={} comparator={} />
β _.intersectionWith<Join array={} separator={} />
β _.join<Last array={} />
β _.last<LastIndexOf array={} value={} fromIndex={} />
β _.lastIndexOf<Nth array={} n={} />
β _.nth<Pull array={} values={} />
β _.pull<PullAll array={} values={} />
β _.pullAll<PullAllBy array={} values={} iteratee={} />
β _.pullAllBy<PullAllWith array={} values={} comparator={} />
β _.pullAllWith<PullAt array={} indexes={} />
β _.pullAt<Remove array={} predicate={} />
β _.remove<Reverse array={} />
β _.reverse<Slice array={} start={} end={} />
β _.slice<SortedIndex array={} value={} />
β _.sortedIndex<SortedIndexBy array={} value={} iteratee={} />
β _.sortedIndexBy<SortedIndexOf array={} value={} />
β _.sortedIndexOf<SortedLastIndex array={} value={} />
β _.sortedLastIndex<SortedLastIndexBy array={} value={} iteratee={} />
β _.sortedLastIndexBy<SortedLastIndexOf array={} value={} />
β _.sortedLastIndexOf<SortedUniq array={} />
β _.sortedUniq<SortedUniqBy array={} iteratee={} />
β _.sortedUniqBy<Tail array={} />
β _.tail<Take array={} n={} />
β _.take<TakeRight array={} n={} />
β _.takeRight<TakeRightWhile array={} predicate={} />
β _.takeRightWhile<TakeWhile array={} predicate={} />
β _.takeWhile<Union arrays={} />
β _.union<UnionBy arrays={} iteratee={} />
β _.unionBy<UnionWith arrays={} comparator={} />
β _.unionWith<Uniq array={} />
β _.uniq<UniqBy array={} iteratee={} />
β _.uniqBy<UniqWith array={} comparator={} />
β _.uniqWith<Unzip array={} />
β _.unzip<UnzipWith array={} iteratee={} />
β _.unzipWith<Without array={} values={} />
β _.without<Xor arrays={} />
β _.xor<XorBy arrays={} iteratee={} />
β _.xorBy<XorWith arrays={} comparator={} />
β _.xorWith<Zip arrays={} />
β _.zip<ZipObject props={} values={} />
β _.zipObject<ZipObjectDeep props={} values={} />
β _.zipObjectDeep<ZipWith arrays={} iteratee={} />
β _.zipWith
<CountBy collection={} iteratee={} />
β _.countBy<Each collection={} iteratee={} />
β _.each<EachRight collection={} iteratee={} />
β _.eachRight<Every collection={} predicate={} />
β _.every<Filter collection={} predicate={} />
β _.filter<Find collection={} predicate={} fromIndex={} />
β _.find<FindLast collection={} predicate={} fromIndex={} />
β _.findLast<FlatMap collection={} iteratee={} />
β _.flatMap<FlatMapDeep collection={} iteratee={} />
β _.flatMapDeep<FlatMapDepth collection={} iteratee={} depth={} />
β _.flatMapDepth<GroupBy collection={} iteratee={} />
β _.groupBy<Includes collection={} value={} fromIndex={} />
β _.includes<InvokeMap collection={} path={} args={} />
β _.invokeMap<KeyBy collection={} iteratee={} />
β _.keyBy<Map collection={} iteratee={} />
β _.map<OrderBy collection={} iteratees={} orders={} />
β _.orderBy<Partition collection={} predicate={} />
β _.partition<Reduce collection={} iteratee={} accumulator={} />
β _.reduce<ReduceRight collection={} iteratee={} accumulator={} />
β _.reduceRight<Reject collection={} predicate={} />
β _.reject<Sample collection={} />
β _.sample<SampleSize collection={} n={} />
β _.sampleSize<Shuffle collection={} />
β _.shuffle<Size collection={} />
β _.size<Some collection={} predicate={} />
β _.some<SortBy collection={} iteratees={} />
β _.sortBy
<Now />
β _.now
<After n={} func={} />
β _.after<Ary func={} n={} />
β _.ary<Before n={} func={} />
β _.before<Bind func={} thisArg={} partials={} />
β _.bind<BindKey object={} key={} partials={} />
β _.bindKey<Curry func={} arity={} />
β _.curry<CurryRight func={} arity={} />
β _.curryRight<Debounce func={} wait={} options={} />
β _.debounce<Defer func={} args={} />
β _.defer<Delay func={} wait={} args={} />
β _.delay<Flip func={} />
β _.flip<Memoize func={} resolver={} />
β _.memoize<Negate predicate={} />
β _.negate<Once func={} />
β _.once<OverArgs func={} transforms={} />
β _.overArgs<Partial func={} partials={} />
β _.partial<PartialRight func={} partials={} />
β _.partialRight<Rearg func={} indexes={} />
β _.rearg<Rest func={} start={} />
β _.rest<Spread func={} start={} />
β _.spread<Throttle func={} wait={} options={} />
β _.throttle<Unary func={} />
β _.unary<Wrap value={} wrapper={} />
β _.wrap
<CastArray value={} />
β _.castArray<Clone value={} />
β _.clone<CloneDeep value={} />
β _.cloneDeep<CloneDeepWith value={} customizer={} />
β _.cloneDeepWith<CloneWith value={} customizer={} />
β _.cloneWith<ConformsTo object={} source={} />
β _.conformsTo<Eq value={} other={} />
β _.eq<Gt value={} other={} />
β _.gt<Gte value={} other={} />
β _.gte<IsArguments value={} />
β _.isArguments<IsArray value={} />
β _.isArray<IsArrayBuffer value={} />
β _.isArrayBuffer<IsArrayLike value={} />
β _.isArrayLike<IsArrayLikeObject value={} />
β _.isArrayLikeObject<IsBoolean value={} />
β _.isBoolean<IsBuffer value={} />
β _.isBuffer<IsDate value={} />
β _.isDate<IsElement value={} />
β _.isElement<IsEmpty value={} />
β _.isEmpty<IsEqual value={} other={} />
β _.isEqual<IsEqualWith value={} other={} customizer={} />
β _.isEqualWith<IsError value={} />
β _.isError<IsFinite value={} />
β _.isFinite<IsFunction value={} />
β _.isFunction<IsInteger value={} />
β _.isInteger<IsLength value={} />
β _.isLength<IsMap value={} />
β _.isMap<IsMatch object={} source={} />
β _.isMatch<IsMatchWith object={} source={} customizer={} />
β _.isMatchWith<IsNaN value={} />
β _.isNaN<IsNative value={} />
β _.isNative<IsNil value={} />
β _.isNil<IsNull value={} />
β _.isNull<IsNumber value={} />
β _.isNumber<IsObject value={} />
β _.isObject<IsObjectLike value={} />
β _.isObjectLike<IsPlainObject value={} />
β _.isPlainObject<IsRegExp value={} />
β _.isRegExp<IsSafeInteger value={} />
β _.isSafeInteger<IsSet value={} />
β _.isSet<IsString value={} />
β _.isString<IsSymbol value={} />
β _.isSymbol<IsTypedArray value={} />
β _.isTypedArray<IsUndefined value={} />
β _.isUndefined<IsWeakMap value={} />
β _.isWeakMap<IsWeakSet value={} />
β _.isWeakSet<Lt value={} other={} />
β _.lt<Lte value={} other={} />
β _.lte<ToArray value={} />
β _.toArray<ToFinite value={} />
β _.toFinite<ToInteger value={} />
β _.toInteger<ToLength value={} />
β _.toLength<ToNumber value={} />
β _.toNumber<ToPlainObject value={} />
β _.toPlainObject<ToSafeInteger value={} />
β _.toSafeInteger<ToString value={} />
β _.toString
<Add augend={} addend={} />
β _.add<Ceil number={} precision={} />
β _.ceil<Divide dividend={} divisor={} />
β _.divide<Floor number={} precision={} />
β _.floor<Max array={} />
β _.max<MaxBy array={} iteratee={} />
β _.maxBy<Mean array={} />
β _.mean<MeanBy array={} iteratee={} />
β _.meanBy<Min array={} />
β _.min<MinBy array={} iteratee={} />
β _.minBy<Multiply multiplier={} multiplicand={} />
β _.multiply<Round number={} precision={} />
β _.round<Subtract minuend={} subtrahend={} />
β _.subtract<Sum array={} />
β _.sum<SumBy array={} iteratee={} />
β _.sumBy
<Clamp number={} lower={} upper={} />
β _.clamp<InRange number={} start={} end={} />
β _.inRange<Random lower={} upper={} floating={} />
β _.random
<Assign object={} sources={} />
β _.assign<AssignWith object={} sources={} customizer={} />
β _.assignWith<At object={} paths={} />
β _.at<Create prototype={} properties={} />
β _.create<Defaults object={} sources={} />
β _.defaults<DefaultsDeep object={} sources={} />
β _.defaultsDeep<Entries object={} />
β _.entries<EntriesIn object={} />
β _.entriesIn<Extend object={} sources={} />
β _.extend<ExtendWith object={} sources={} customizer={} />
β _.extendWith<FindKey object={} predicate={} />
β _.findKey<FindLastKey object={} predicate={} />
β _.findLastKey<ForIn object={} iteratee={} />
β _.forIn<ForInRight object={} iteratee={} />
β _.forInRight<ForOwn object={} iteratee={} />
β _.forOwn<ForOwnRight object={} iteratee={} />
β _.forOwnRight<Functions object={} />
β _.functions<FunctionsIn object={} />
β _.functionsIn<Get object={} path={} defaultValue={} />
β _.get<Has object={} path={} />
β _.has<HasIn object={} path={} />
β _.hasIn<Invert object={} />
β _.invert<InvertBy object={} iteratee={} />
β _.invertBy<Invoke object={} path={} args={} />
β _.invoke<Keys object={} />
β _.keys<KeysIn object={} />
β _.keysIn<MapKeys object={} iteratee={} />
β _.mapKeys<MapValues object={} iteratee={} />
β _.mapValues<Merge object={} sources={} />
β _.merge<MergeWith object={} sources={} customizer={} />
β _.mergeWith<Omit object={} paths={} />
β _.omit<OmitBy object={} predicate={} />
β _.omitBy<Pick object={} paths={} />
β _.pick<PickBy object={} predicate={} />
β _.pickBy<Result object={} path={} defaultValue={} />
β _.result<Set object={} path={} value={} />
β _.set<SetWith object={} path={} value={} customizer={} />
β _.setWith<Transform object={} iteratee={} accumulator={} />
β _.transform<Unset object={} path={} />
β _.unset<Update object={} path={} updater={} />
β _.update<UpdateWith object={} path={} updater={} customizer={} />
β _.updateWith<Values object={} />
β _.values<ValuesIn object={} />
β _.valuesIn
<Chain value={} />
β _.chain<Tap value={} interceptor={} />
β _.tap<Thru value={} interceptor={} />
β _.thru
<CamelCase string={} />
β _.camelCase<Capitalize string={} />
β _.capitalize<Deburr string={} />
β _.deburr<EndsWith string={} target={} position={} />
β _.endsWith<Escape string={} />
β _.escape<EscapeRegExp string={} />
β _.escapeRegExp<KebabCase string={} />
β _.kebabCase<LowerCase string={} />
β _.lowerCase<LowerFirst string={} />
β _.lowerFirst<Pad string={} length={} chars={} />
β _.pad<PadEnd string={} length={} chars={} />
β _.padEnd<PadStart string={} length={} chars={} />
β _.padStart<ParseInt string={} radix={} />
β _.parseInt<Repeat string={} n={} />
β _.repeat<Replace string={} pattern={} replacement={} />
β _.replace<SnakeCase string={} />
β _.snakeCase<Split string={} separator={} limit={} />
β _.split<StartCase string={} />
β _.startCase<StartsWith string={} target={} position={} />
β _.startsWith<Template string={} options={} />
β _.template<ToLower string={} />
β _.toLower<ToUpper string={} />
β _.toUpper<Trim string={} chars={} />
β _.trim<TrimEnd string={} chars={} />
β _.trimEnd<TrimStart string={} chars={} />
β _.trimStart<Truncate string={} options={} />
β _.truncate<Unescape string={} />
β _.unescape<UpperCase string={} />
β _.upperCase<UpperFirst string={} />
β _.upperFirst<Words string={} pattern={} />
β _.words
<Attempt func={} args={} />
β _.attempt<BindAll object={} methodNames={} />
β _.bindAll<Cond pairs={} />
β _.cond<Conforms source={} />
β _.conforms<Constant value={} />
β _.constant<DefaultTo value={} defaultValue={} />
β _.defaultTo<Flow funcs={} />
β _.flow<FlowRight funcs={} />
β _.flowRight<Identity value={} />
β _.identity<Iteratee func={} />
β _.iteratee<Matches source={} />
β _.matches<MatchesProperty path={} srcValue={} />
β _.matchesProperty<Method path={} args={} />
β _.method<MethodOf object={} args={} />
β _.methodOf<Mixin object={} source={} options={} />
β _.mixin<Noop />
β _.noop<NthArg n={} />
β _.nthArg<Over iteratees={} />
β _.over<OverEvery predicates={} />
β _.overEvery<OverSome predicates={} />
β _.overSome<Property path={} />
β _.property<PropertyOf object={} />
β _.propertyOf<Range start={} end={} step={} />
β _.range<RangeRight start={} end={} step={} />
β _.rangeRight<StubArray />
β _.stubArray<StubFalse />
β _.stubFalse<StubObject />
β _.stubObject<StubString />
β _.stubString<StubTrue />
β _.stubTrue<Times n={} iteratee={} />
β _.times<ToPath value={} />
β _.toPath<UniqueId prefix={} />
β _.uniqueId
MIT
Patreon - Supporters β¨