Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1
A declarative component for conditional rendering.
License
uiwjs/react-only-when
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A declarative component for conditional rendering. Copyreact-only-when, let it support TypeScript.
$ npm install --save @uiw/react-only-when
importOnlyfrom'@uiw/react-only-when'<Onlywhen={true}><h1>Here I Am</h1></Only>
import{If}from'@uiw/react-only-when/if'<Ifcondition={props.error}><h1>{props.error}</h1></If>
import{Switch,Case,Default}from'@uiw/react-only-when/switch'<Switch><Casecondition={age<6}>preschool</Case><Casecondition={age>=6}>primary school</Case><Default>you graduated</Default></Switch>
React component that renders the children if thecondition prop istrue.
import{If}from'@uiw/react-only-when';// Orimport{If}from'@uiw/react-only-when/if'<div><Ifcondition={props.error}render={()=>(<h1>{props.error}</h1>)}/><Ifcondition={props.error}><h1>{props.error}</h1></If></div>
Or you could just use plain #"auto" data-snippet-clipboard-copy-content="<div> {props.error && ( <h1>{props.error}</h1> )}</div>">
<div>{props.error&&(<h1>{props.error}</h1>)}</div>
Only Example
importReact,{useState}from'react';importOnlyfrom'@uiw/react-only-when';exportdefaultfunctionApp(){const[show,setShow]=useState(true)return(<divclassName="app"><buttononClick={()=>setShow(!show)}>Toggle</button><Onlywhen={show}><h1>Here I Am</h1></Only></div>)}
import{Switch,Case,Default}from'@uiw/react-only-when/switch'<Switch><Casecondition={age<6}>preschool</Case><Caseas="div"condition={age>=6}>primary school</Case><Default>you graduated</Default></Switch>
importReact,{useState,Fragment}from'react';import{Switch,Case,Default}from'@uiw/react-only-when/switch'exportdefaultfunctionApp(){const[age,setAge]=useState(19)return(<Fragment><inputtype="range"onChange={(evn)=>setAge(Number(evn.target.value))}/>{age}<br/><Switch><Casecondition={age<6}>Preschool</Case><Casecondition={age>=6&&age<18}>Primary school</Case><Casecondition={age>=18&&age<60}>Went to college</Case><Default>you graduated</Default></Switch></Fragment>);}
Defaults to specifying a wrapped HTML Element.
importReact,{useState,Fragment}from'react';import{Switch,Case,Default}from'@uiw/react-only-when/switch'exportdefaultfunctionApp(){const[age,setAge]=useState(19)return(<Fragment><inputtype="range"onChange={(evn)=>setAge(Number(evn.target.value))}/>{age}<br/><Switch><Caseas="span"condition={age<6}>Preschool</Case><Caseas="em"condition={age>=6&&age<18}>Primary school</Case><Caseas="div"condition={age>=18&&age<60}>Went to college</Case><Defaultas="p">you graduated</Default></Switch></Fragment>);}
| prop name | type | default | isRequired | description |
|---|---|---|---|---|
| children | react element | null | true | A single child element |
| when | bool | false | true | When true, children will rendered as is |
| hiddenMode | string | null | false | Determines how children should be hidden |
| className | string | w-hidden | false | This is working in combination withhiddenMode={"css"} |
hiddenMode enum
| hiddenMode | description |
|---|---|
null | Will not render the child |
display | Will render the child withdisplay:none |
visibility | Will render the child withvisibility:hidden |
css | Will render the child with a CSS class (you can pass it a customclassName prop) |
import{ReactElement}from'react';import{FC,PropsWithChildren}from'react';exportinterfaceIfProps{readonlycondition?:boolean;readonlyrender?:()=>ReactElement;}exportdeclareconstIf:FC<PropsWithChildren<IfProps>>;
importtype{FC,PropsWithChildren}from'react';exportconstSwitch:FC<PropsWithChildren<{}>>;typeTagType=React.ElementType|keyofJSX.IntrinsicElements;interfaceCaseElementProps<TextendsTagType>{as?:T;readonlycondition?:boolean;}exporttypeCaseProps<TextendsTagType>=CaseElementProps<T>&React.ComponentPropsWithoutRef<T>;exportconstCase:<TextendsTagType>(props:CaseProps<T>)=>any;exportconstDefault:<TextendsTagType>(props:Omit<CaseProps<T>,'condition'>)=>import("react/jsx-runtime").JSX.Element;
Runs the project in development mode.
# Step 1, run first, listen to the component compile and output the .js file# listen for compilation output type .d.ts filenpm run watch# Step 2, development mode, listen to compile preview website instancenpm run start
production
Builds the app for production to the build folder.
npm run build
The build is minified and the filenames include the hashes.Your app is ready to be deployed!
As always, thanks to our amazing contributors!
Made withcontributors.
About
A declarative component for conditional rendering.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.