|
1 |
| -react-loading-switch |
| 1 | +react-loading-switch 🐶 |
2 | 2 | ==
|
3 | 3 |
|
4 | 4 | React component API for easily composing the render logic surrounding react-apollo data fetching, loading, and error handling.
|
5 | 5 |
|
6 |
| -Getting Started |
| 6 | +Compatible with React, React Native, React Web, React anything! |
| 7 | + |
| 8 | +The Problem: |
7 | 9 | --
|
8 | 10 |
|
9 |
| -```shell |
10 |
| -npm i --save react-loading-switch |
| 11 | +### Data-related conditional rendering in every component |
| 12 | + |
| 13 | +In our experience, re-writing identical or similar logic in every component is bad. |
| 14 | + |
| 15 | + - ❌ Error prone. |
| 16 | + - ❌ Multiple programming styles result in very different-looking code. |
| 17 | + - ❌ Difficult to digest at a glance. |
| 18 | + - ❌ Problem grows as codebase grows. |
| 19 | + |
| 20 | +#### This sucks 👎 |
| 21 | + |
| 22 | +```js |
| 23 | +render = () => { |
| 24 | + const { loading, error, puppy } = this.props.data |
| 25 | + |
| 26 | + if (error) { |
| 27 | + return <RenderError error={error} /> |
| 28 | + } |
| 29 | + |
| 30 | + if (!puppy) { |
| 31 | + if (loading) { |
| 32 | + return <RenderLoading /> |
| 33 | + } |
| 34 | + |
| 35 | + return <RenderError error={new Error('Could not find puppy!')} /> |
| 36 | + } |
| 37 | + |
| 38 | + return ( |
| 39 | + <View>{ `Finally the puppy is here! ${puppy.id}` }</View> |
| 40 | + ) |
| 41 | +} |
11 | 42 | ```
|
12 | 43 |
|
13 |
| -Example |
14 |
| --- |
| 44 | +### Instead, compose your rendering with `react-loading-switch`! |
| 45 | + |
| 46 | + - ✅ Consistent JSX component API. |
| 47 | + - ✅ Easy to digest at a glance. |
| 48 | + - ✅ Extensible & Functional |
| 49 | + - ✅ Centralized default configuration across multiple components if desired. |
| 50 | + - ✅ It's just a react component. Wrap it with default props if you want to share functionality between components. |
| 51 | + |
| 52 | +#### Meet `<LoadingSwitch />` 👍 |
15 | 53 |
|
16 | 54 | ```js
|
| 55 | +import LoadingSwitch from 'react-loading-switch' |
| 56 | + |
17 | 57 | render() {
|
18 |
| - const { loading, error, media, artist } = this.props.data |
| 58 | + const { loading, error, puppy } = this.props.data |
19 | 59 |
|
20 | 60 | return (
|
21 | 61 | <LoadingSwitch
|
22 | 62 | error={error}
|
23 |
| - errorWhenMissing={() => new Error('Missing required data!')} |
| 63 | + errorWhenMissing={() => new Error('Missing puppy data!')} |
24 | 64 | loading={loading}
|
25 | 65 | renderError={(error) => <DataError error={error} />}
|
26 | 66 | renderLoading={() => <Loading />}
|
27 |
| - require={media && artist} |
| 67 | + require={puppy} |
28 | 68 | >
|
29 | 69 | { () => (
|
30 |
| - <Text>This is rendered when have the data! { media.id }</Text> |
| 70 | + <View>{ `The puppy data is here! ${puppy.id}` }</View> |
31 | 71 | ) }
|
32 | 72 | </LoadingSwitch>
|
33 | 73 | )
|
34 | 74 | }
|
35 | 75 | ```
|
36 | 76 |
|
| 77 | +#### DRY it up by wrapping with some default props 🎉 |
| 78 | + |
| 79 | +```js |
| 80 | +import MyLoadingSwitch from '../MyLoadingSwitch' |
| 81 | + |
| 82 | +render() { |
| 83 | + const { loading, error, puppy } = this.props.data |
| 84 | + |
| 85 | + return ( |
| 86 | + <MyLoadingSwitch |
| 87 | + error={error} |
| 88 | + loading={loading} |
| 89 | + require={puppy} |
| 90 | + > |
| 91 | + { () => ( |
| 92 | + <View>{ `The puppy data is here! ${puppy.id}` }</View> |
| 93 | + ) } |
| 94 | + </MyLoadingSwitch> |
| 95 | + ) |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +#### With React-Apollo's shiny new `<Query />` components :godmode: |
| 100 | + |
| 101 | +```js |
| 102 | +import LoadingSwitch from 'react-loading-switch' |
| 103 | +import { Query } from 'react-apollo' |
| 104 | + |
| 105 | +const GET_PUPPY = gql` |
| 106 | + query puppy($puppyId: ID!) { |
| 107 | + puppy(id: $puppyId) { |
| 108 | + id |
| 109 | + name |
| 110 | + birthday |
| 111 | + } |
| 112 | + } |
| 113 | +`; |
| 114 | + |
| 115 | +const PuppyBirthday = ({ puppyId }) => ( |
| 116 | + <Query query={GET_PUPPY} variables={{ puppyId }}> |
| 117 | + {({ loading, error, data: { puppy } }) => ( |
| 118 | + <LoadingSwitch |
| 119 | + error={error} |
| 120 | + errorWhenMissing={() => new Error('Missing puppy birthday data!')} |
| 121 | + loading={loading} |
| 122 | + renderError={(error) => <PuppyBirthdayError error={error} />} |
| 123 | + renderLoading={() => <PuppyBirthdayLoading />} |
| 124 | + require={puppy} |
| 125 | + > |
| 126 | + { () => ( |
| 127 | + <View>{ `${name}'s birthday is ${puppy.birthday}!` }</View> |
| 128 | + ) } |
| 129 | + </LoadingSwitch> |
| 130 | + )} |
| 131 | + </Query> |
| 132 | +) |
| 133 | +``` |
| 134 | + |
| 135 | +#### Require multiple things! It uses JavaScript's truthy/falsey checking. |
| 136 | + |
| 137 | +*Remember falsey in JavaScript: `false || null || undefined || 0 || '' || NaN` * :neckbeard: |
| 138 | + |
| 139 | +```js |
| 140 | +render() { |
| 141 | + const { loading, error, someData, moreData } = this.props.data |
| 142 | + |
| 143 | + return ( |
| 144 | + <MyLoadingSwitch |
| 145 | + error={error} |
| 146 | + loading={loading} |
| 147 | + require={someData && moreData && moreData.hasTheRequiredThing} |
| 148 | + > |
| 149 | + { () => ( |
| 150 | + <View>{ moreData.hasTheRequiredThing }</View> |
| 151 | + ) } |
| 152 | + </MyLoadingSwitch> |
| 153 | + ) |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +Getting Started |
| 158 | +-- |
| 159 | + |
| 160 | +```shell |
| 161 | +npm i --save react-loading-switch |
| 162 | +``` |
| 163 | + |
37 | 164 | API
|
38 | 165 | --
|
39 | 166 |
|
|
0 commit comments