Code Monkey home page Code Monkey logo

react-maybe's Introduction

react-maybe

Render one of two React components

Build Status

react-maybe is built behind a monad which conditionally displays one of two components based on the value contained in the of prop. If the value is one of null, undefined or false, the orElse component will be displayed. In all other cases the component passed to the either prop will be displayed.

The value in the of prop can be altered through the use map prop. The map prop could be a function or an array of functions. The value of of will be passed to the first map function, each additional map function will receive the return value of the previous one in the array. The return value of the last map funtion will then be used to determine which component to render.

Installation

Installation is currently available via npm:

$ npm i -S react-maybe

Usage

Basic

A basic react-maybe implementation will display one of two components based on the value of the of prop:

import Maybe from 'react-maybe';

const Component = ({ showChevronUp }) => (
  <div>
    <Maybe of={showChevronUp} either={<ChevronUp />} orElse={<ChevronDown />} />
  </div>
);
Advanced

A more advanced react-maybe implementation can alter the value of the of prop through use of the map prop:

import Maybe from 'react-maybe';

const sampleUser = { name: 'Maybe', isActive: false };
const isActive = user => user.isActive;

const Component = () => (
  <div>
    <Maybe of={sampleUser} map={isActive} either={<ActiveUser />} orElse={<InactiveUser />} />
  </div>
);

The above will render the orElse as our sample user is inactive

or

import Maybe from 'react-maybe';

const sampleUser = { name: 'Maybe', isActive: true };
const isActive = user => (user.isActive ? user : false);
const hasPhoneNumber = user => !!user.phone;

const Component = () => (
  <div>
    <Maybe
      of={sampleUser}
      map={[isActive, hasPhoneNumber]}
      either={<ActiveUserWithPhone />}
      orElse={<UserWithoutPhone />}
    />
  </div>
);

The above example will render the orElse component as our user is active, but has no phone number

Properties

  • of

    The value that the monad will contain.

  • map

    Accepts a function or an array of functions which has the ability to transform the value contained in the of prop.

  • either

    Accepts a component that will be rendered in all cases where the value of the of prop is not equal to undefined, null, or false.

  • orElse

    Accepts a component that will be rendered when the value of the of prop is equal to undefined, null, or false.

Running tests

You can either run the tests with npm or docker.

NPM
$ npm i
$ npm test
Docker
$ docker-compose run --rm app npm i
$ docker-compose up

react-maybe's People

Contributors

strange-developer avatar hvolschenk avatar

Stargazers

明明 avatar JimmyLv_吕立青 avatar Giancarlo Soverini avatar Pavel Tchaikovsky avatar Paweł Lula avatar Vincent avatar  avatar

Watchers

James Cloos avatar Vincent avatar  avatar  avatar

react-maybe's Issues

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two-Factor Authentication, make configure the auth-only level is supported. semantic-release cannot publish with the default auth-and-writes level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot 📦🚀

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.