• Stars
    star
    356
  • Rank 114,986 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⚛️ 🔧 Lodash as React components

react-lodash build status npm

Use any lodash function as a React component

Example

Without

import react from 'react'

array && array.length ? (
  <ul>
    {array.map(i => (
      <li key={i}>{i}</li>
    ))}
  </ul>
) : (
  'Empty list'
)

With

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>
  )}
/>

Demo

You can play with react-lodash on CodeSandbox

Edit react-lodash-example

Install

npm install react-lodash

Introduction

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 :)

API

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>}
/>

Importing

You can either use named imports or individually import components

import { IsEmpty } from 'react-lodash'
import IsEmpty from 'react-lodash/lib/IsEmpty'

Components

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 ;)

Array

Collection

  • <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

Date

Function

  • <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

Lang

Math

  • <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

Number

  • <Clamp number={} lower={} upper={} />_.clamp
  • <InRange number={} start={} end={} />_.inRange
  • <Random lower={} upper={} floating={} />_.random

Object

  • <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

Seq

  • <Chain value={} />_.chain
  • <Tap value={} interceptor={} />_.tap
  • <Thru value={} interceptor={} />_.thru

String

Util

License

MIT

Patreon - Supporters

More Repositories

1

json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
JavaScript
70,794
star
2

husky

Git hooks made easy 🐶 woof!
JavaScript
31,302
star
3

lowdb

Simple and fast JSON database
JavaScript
20,699
star
4

hotel

🏩 A simple process manager for developers. Start apps from your browser and access them using local domains
JavaScript
9,953
star
5

jsonplaceholder

A simple online fake REST API server
HTML
4,945
star
6

tlapse

📷 Create a timelapse of your web development... or just automatically take screenshots of your hard work ;)
JavaScript
2,026
star
7

xv

🙅‍♀️ ✌️ fastest test runner
JavaScript
829
star
8

pegasus

Load JSON while still loading other scripts
JavaScript
702
star
9

katon

(use hotel instead)
JavaScript
683
star
10

steno

Super fast async file writer with atomic write ⚡
JavaScript
664
star
11

react-fake-props

🔮 Magically generate fake props for your React tests
JavaScript
628
star
12

fetchival

Easy window.fetch requests
JavaScript
517
star
13

lodash-id

Makes it easy to manipulate id-based resources with lodash or lowdb
JavaScript
467
star
14

stop-server

📱 Shut down your computer with your phone
JavaScript
341
star
15

demo

A demo repository for My JSON Server (Alpha)
326
star
16

pinst

🍺 dev only postinstall hooks (package.json)
JavaScript
256
star
17

please-upgrade-node

💁 Show a message to your users to upgrade Node instead of a stacktrace
JavaScript
238
star
18

jsop

JSON file reader/writer (powered by Object.observe)
JavaScript
205
star
19

mistcss

Write atomic React components using only CSS! (JS-from-CSS™) 🌬️
JavaScript
168
star
20

husky-4-to-8

Quickly migrate your hooks from husky v4 to husky@latest
JavaScript
126
star
21

user-startup

Auto start commands when you log in (cross-platform)
JavaScript
123
star
22

cult

cult monitors gulpfile changes and reloads gulp
JavaScript
118
star
23

logan

Mini template system for the console and colors
JavaScript
80
star
24

bg.nvim

Automatically sync your terminal background with your colorscheme 🎆
Lua
70
star
25

minihost

Easily start and access servers
JavaScript
45
star
26

ghwn

Get desktop notifications for new issues, comments, stars... (no installation required)
HTML
42
star
27

shoutjs

Make your ShellJS commands explicit and get a beautiful output
JavaScript
29
star
28

homerun

Turn npm package scripts into CLI commands
JavaScript
24
star
29

server-ready

Know when a server is ready to receive requests
JavaScript
20
star
30

husky-init

JavaScript
20
star
31

backbone-pegasus

Load models and collections data while loading Backbone
JavaScript
19
star
32

server-ready-cli

Run commands only when a server is available
JavaScript
18
star
33

typicode.github.io

HTML
7
star
34

eslint-config

JavaScript
7
star