Skip to content

Commit c75e0dd

Browse files
authored
Merge pull request #2 from Vydia/update-readme-with-examples-and-more-importantly-emojis
💵 update readme with examples and more importantly emojis 💵
2 parents 7093422 + ce2acb6 commit c75e0dd

File tree

1 file changed

+137
-10
lines changed

1 file changed

+137
-10
lines changed

README.md

Lines changed: 137 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,166 @@
1-
react-loading-switch
1+
react-loading-switch 🐶
22
==
33

44
React component API for easily composing the render logic surrounding react-apollo data fetching, loading, and error handling.
55

6-
Getting Started
6+
Compatible with React, React Native, React Web, React anything!
7+
8+
The Problem:
79
--
810

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+
}
1142
```
1243

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 />` 👍
1553

1654
```js
55+
import LoadingSwitch from 'react-loading-switch'
56+
1757
render() {
18-
const { loading, error, media, artist } = this.props.data
58+
const { loading, error, puppy } = this.props.data
1959

2060
return (
2161
<LoadingSwitch
2262
error={error}
23-
errorWhenMissing={() => new Error('Missing required data!')}
63+
errorWhenMissing={() => new Error('Missing puppy data!')}
2464
loading={loading}
2565
renderError={(error) => <DataError error={error} />}
2666
renderLoading={() => <Loading />}
27-
require={media && artist}
67+
require={puppy}
2868
>
2969
{ () => (
30-
<Text>This is rendered when have the data! { media.id }</Text>
70+
<View>{ `The puppy data is here! ${puppy.id}` }</View>
3171
) }
3272
</LoadingSwitch>
3373
)
3474
}
3575
```
3676

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+
37164
API
38165
--
39166

0 commit comments

Comments
 (0)