• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

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

Repository Details

Maybe render a React component, maybe not 😮

Maybe render a React component, maybe not 😮

Build Status dependencies Status Code Climate JavaScript Style Guide

// Get it!
yarn add mayre
npm install --save mayre

While working with React you'll find yourself making conditionals components quite a lot of times. And they're always the same: a component which upon a set of conditions may render or just return null (or short-circuit it).

Here comes Mayre (Maybe render)! A very simple and super light (2kb) component to tackle this issue from the jsx side.

Compatible with React, React-Native and ReactVR.

Usage

Maybe

There are three props you can use: of, when and with.

<Mayre
  of={MyComponent}
  when={props.something > 10}
  with={{ some: 'thing' }}
/>

Note that of can be a component instance or declaration. And when can be boolean or a function.

<Mayre
  of={<p>Something</p>}
  when={() => checkWhatever()}
/>

Either

But not only this! Conditional rendering isn't only about mount this component or not. We can also use Mayre to render either this element or the other.

<Mayre
  of={<p>Either this</p>}
  or={<p>Or this one</p>}
  when={whateverCondition}
/>

If a with prop is provided it'll be applied to both of them. If you want to specify special props for each of them use orWith.

<Mayre
  of={<p>Either this</p>}
  or={<p>Or this one</p>}
  when={whateverCondition}
  with={{ appliedTo: 'of' }}
  orWith={{ appliedTo: 'this will used by or element' }}
/>

Auto props picking

Most of the times the component rendered by Mayre is a subset of a bigger one. Hence, it's using a selection of the parent props. That's why Mayre has a special syntax to pick the props you need to while passing them down.

<Mayre
  of={MyComponent}
  when={props.something > 10}
  with={[props, 'thisProps', 'andThisOther']}
/>

Same can be applied for orWith attribute.

Props

Name Required Default Type Comment
of Yes - func, element The React component to be rendered
or No null func, element The React component rendered instead of of
when No false bool, func The render condition
with No {} object, array Props to be passed to of/or component
orWith No {} object, array Props to be passed to or component

Benefit

Stop doing this:

// no more dumb render methods pollution
const renderSomething = (canRender, propsFromParent) => {
  if (!canRender) return null

  return <Something {...propsFromParent} />
}

const Parent = (props) => (
  <div>
    {renderSomething(props.a === props.b, props)}
  </div>
)

More Repositories

1

logatim

Logatim is an isomorphic logger which implements log levels and ANSI styles.
JavaScript
193
star
2

reinput

A React Native TextInput with material style 😎
JavaScript
55
star
3

react-native-dual

ScrollView, FlatList, SectionList and ListView with dual background
JavaScript
27
star
4

rfm

Request for maintainers explorer
TypeScript
19
star
5

tsil

Flatten and deflatten objects. Work with lists 📝
JavaScript
14
star
6

badgifier

Generate badges without third-party services 🛡
JavaScript
10
star
7

finite-state-machine-demo

Find the slides here -->
JavaScript
9
star
8

talks

Slides and other stuff related to my public talks ;)
8
star
9

pods

Pods architecture showroom
JavaScript
8
star
10

sospedra.me

Personal website source code
TypeScript
6
star
11

redux-persist-migrate-demo

A redux-persist-migrate show off
JavaScript
6
star
12

which-key-code

Which keys map to what keyboard code?
HTML
5
star
13

len

Safely access arrays' length property 🍥
JavaScript
5
star
14

coronamail

Spreading love in times of virus
TypeScript
5
star
15

semantic-password-generator

Secure passwords that humans can read 🗝
CSS
4
star
16

schemative

Declarative objects with default values, transformers, mutators and propTypes
JavaScript
3
star
17

the-sagas-cookbook

JavaScript
2
star
18

rundos

Run Dos Project
JavaScript
2
star
19

banana-coin-demo

Banana coin demo 🍌
JavaScript
1
star
20

pulse

Explore the real-time insights of Twitter
JavaScript
1
star
21

parallax

TypeScript
1
star
22

babel-plugin-node

Make any environment node compatible
JavaScript
1
star
23

kondolint

1
star
24

csb-6rn7s

HTML
1
star
25

react-native-animation-labs

TypeScript
1
star
26

peecto-handshake-public

JavaScript
1
star
27

expo-blob-error-demo

TypeScript
1
star
28

embracestall

Java
1
star
29

fullstacklon2017

JavaScript
1
star
30

peecto

TypeScript
1
star
31

bonfire

TypeScript
1
star
32

gulp-haml-sass

Fast bootstraping for the web stack: sass, haml and -of course- js.
JavaScript
1
star
33

peecto-handshake

JavaScript
1
star
34

olympics

The Olympics Score
TypeScript
1
star