Skip to content

Repository files navigation

react-only-when

A declarative component for conditional rendering

NPMJavaScript Style Guide

Install

npm install --save react-only-when

Usage

importOnlyfrom'react-only-when'<Onlywhen={true}><h1>Here I Am</h1></Only>

Full Example

importReactfrom'react';importOnlyfrom'react-only-when'classAppextendsReact.Component{state={show: true};toggle=()=>this.setState(state=>({show: !state.show}));render(){const{ show }=this.state;return(<divclassName="app"><buttononClick={this.toggle}>Toggle</button><Onlywhen={show}><h1>Here I Am</h1></Only></div>);}}

props

prop nametypedefaultisRequireddescription
childrenreact elementnulltrueA single child element
whenboolfalsetrueWhen true, children will rendered as is
hiddenModestring"withNull"falseDetermines how children should be hidden
classNamestring"r-o_hidden"falseThis is working in combination with hiddenMode={"withCss"}

hiddenMode enum

hiddenModedescription
"withNull"Will not render the child
"withDisplay"Will render the child with display:none
"withVisibility"Will render the child with visibility:hidden
"withCss"Will render the child with a CSS class (you can pass it a custom className prop)

License

MIT © sag1v

About

A declarative component for conditional rendering

Resources

Stars

7 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages