|
| 1 | +--- |
| 2 | +id: switch-case-operator |
| 3 | +sidebar_label: Switch case operator |
| 4 | +title: Switch Case Operator |
| 5 | +description: Switch case operator | React Patterns, techniques, tips and tricks in development for Ract developer. |
| 6 | +keywords: ['switch case operator', 'react switch case operator', 'reactpatterns', 'react patterns', 'reactjspatterns', 'reactjs patterns', 'react', 'reactjs', 'react techniques', 'react tips and tricks'] |
| 7 | +version: Switch case operator |
| 8 | +image: /img/reactpatterns-cover.png |
| 9 | +--- |
| 10 | + |
| 11 | +Now there might be cases where you have multiple conditional renderings. The conditional rendering could apply based on different states. |
| 12 | + |
| 13 | +Let's imagine a notification component that can render an error, warning or info component based on the input state. You can use a switch case operator to handle the conditional rendering of these multiple states. |
| 14 | + |
| 15 | +```jsx |
| 16 | +function Notification({ text, state }) { |
| 17 | + switch(state) { |
| 18 | + case 'info': |
| 19 | + return <Info text={text} /> |
| 20 | + case 'warning': |
| 21 | + return <Warning text={text} /> |
| 22 | + case 'error': |
| 23 | + return <Error text={text} /> |
| 24 | + default: |
| 25 | + return null |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +Please note that you always have to use the `default` for the switch case operator. In React a component has always to return an element or `null`. |
| 31 | + |
| 32 | +As a little information, when a component has a conditional rendering based on a state, it makes sense to describe the interface of the component with `React.PropTypes`. |
| 33 | + |
| 34 | +```jsx |
| 35 | +function Notification({ text, state }) { |
| 36 | + switch(state) { |
| 37 | + case 'info': |
| 38 | + return <Info text={text} /> |
| 39 | + case 'warning': |
| 40 | + return <Warning text={text} /> |
| 41 | + case 'error': |
| 42 | + return <Error text={text} /> |
| 43 | + default: |
| 44 | + return null |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +Notification.propTypes = { |
| 49 | + text: React.PropTypes.string, |
| 50 | + state: React.PropTypes.oneOf(['info', 'warning', 'error']) |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +Now you have one generic component to show different kinds of notifications. Based on the state prop the component could have different looks. An error should be red, a warning should be yellow and an info should be blue. |
| 55 | + |
| 56 | +An alternative way would be to inline the switch case. Therefore you would need a self invoking JavaScript function. |
| 57 | + |
| 58 | +```jsx |
| 59 | +function Notification({ text, state }) { |
| 60 | + return ( |
| 61 | + <div> |
| 62 | + {(() => { |
| 63 | + switch(state) { |
| 64 | + case 'info': |
| 65 | + return <Info text={text} /> |
| 66 | + case 'warning': |
| 67 | + return <Warning text={text} /> |
| 68 | + case 'error': |
| 69 | + return <Error text={text} /> |
| 70 | + default: |
| 71 | + return null |
| 72 | + } |
| 73 | + })()} |
| 74 | + </div> |
| 75 | + ) |
| 76 | +} |
| 77 | +``` |
0 commit comments