All Projects → sospedra → Mayre

sospedra / Mayre

Licence: mit
Maybe render a React component, maybe not 😮

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Mayre

Polyfill Php56
This component provides functions unavailable in releases prior to PHP 5.6.
Stars: ✭ 1,470 (+1189.47%)
Mutual labels:  component
React Scroll Parallax
🔮 React components to create parallax scroll effects for banners, images or any other DOM elements
Stars: ✭ 1,699 (+1390.35%)
Mutual labels:  component
Expected
What did you expect?
Stars: ✭ 113 (-0.88%)
Mutual labels:  monad
Var Exporter
The VarExporter component allows exporting any serializable PHP data structure to plain PHP code. While doing so, it preserves all the semantics associated with the serialization mechanism of PHP (__wakeup, __sleep, Serializable).
Stars: ✭ 1,616 (+1317.54%)
Mutual labels:  component
React Multi Select
A Multi Select component built with and for React
Stars: ✭ 111 (-2.63%)
Mutual labels:  component
React Lego
一种特别注重扩展和复用的 React 组件编写规则。
Stars: ✭ 112 (-1.75%)
Mutual labels:  component
Crocks
A collection of well known Algebraic Data Types for your utter enjoyment.
Stars: ✭ 1,501 (+1216.67%)
Mutual labels:  monad
Router
🍭灵活的组件化路由框架.
Stars: ✭ 1,502 (+1217.54%)
Mutual labels:  component
Ember Toggle
Checkbox based Toggle Switches for Ember
Stars: ✭ 111 (-2.63%)
Mutual labels:  component
Alert
⚠️ Alert is a simple notification that appears on the top of the screen.
Stars: ✭ 113 (-0.88%)
Mutual labels:  component
Omi Electron
🚀Build cross platform desktop apps with Omi.js and Electron.js 基于Omi.js和Electron.js构建跨平台的桌面应用
Stars: ✭ 110 (-3.51%)
Mutual labels:  component
Realtaiizor
C# WinForm UI/UX Component Library
Stars: ✭ 109 (-4.39%)
Mutual labels:  component
Vue Grid
A flexible grid component for Vue.js
Stars: ✭ 113 (-0.88%)
Mutual labels:  component
Maz Ui
Stand-alone components library to build your interfaces with Vue.JS & Nuxt.JS
Stars: ✭ 109 (-4.39%)
Mutual labels:  component
Vue Pdf
vue.js pdf viewer
Stars: ✭ 1,700 (+1391.23%)
Mutual labels:  component
Quist Ui
快应用UI组件库 https://jdsecretfe.github.io/quist-ui/
Stars: ✭ 108 (-5.26%)
Mutual labels:  component
Vue Highcharts
The Component of Vue 2.x for highcharts
Stars: ✭ 112 (-1.75%)
Mutual labels:  component
Styled Typography
Typograpy components for react and styled-components
Stars: ✭ 113 (-0.88%)
Mutual labels:  component
Error Handler
The ErrorHandler component provides tools to manage errors and ease debugging PHP code.
Stars: ✭ 1,852 (+1524.56%)
Mutual labels:  component
F
Functional stuff for Python
Stars: ✭ 113 (-0.88%)
Mutual labels:  monad

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>
)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].