npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mayre

v4.0.0

Published

Maybe render a React component, maybe not 😮

Downloads

146

Readme

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