1.0.0 • Published 7 years ago

hoc-react-switcher v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

hoc-react-switcher

CircleCI

what is this?

This is a higher order component ("HOC") which displays one of its possible states depending on a prop value.

install

npm i --save hoc-react-switcher

use

You have to give a map of components and optionally some other informations:

ParameterRequiredDefault valueDescription
propnostateThe prop to watch. It determines which component to display
statesyes{}A map of components
DefaultComponentnoundefinedA component to display when prop value doesn't match one of the states

Component.js

import React, { PropTypes } from 'react'
import switcher from 'hoc-react-switcher'

const FirstComponent = () => {
  return (
    <div>
      FirstComponent
    </div>
  )
}

const SecondComponent = () => {
  return (
    <div>
      SecondComponent
    </div>
  )
}

const DefaultComponent = () => {
  return (
    <div>
      DefaultComponent
    </div>
  )
}

export default switcher({
  prop: 'custom',
  states: {
    0: FirstComponent,
    1: SecondComponent
  },
  DefaultComponent,
})