Skip to content

Commit 776247a

Browse files
committed
Fix code formatting issues from markdown conversion
1 parent ca71a6a commit 776247a

26 files changed

+365
-543
lines changed

.prettierrc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
{
2-
"trailingComma": "none",
3-
"arrowParens": "avoid"
4-
}
1+
{}

src/pages/useAnimation.md

Lines changed: 34 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -9,158 +9,116 @@ sandbox: https://codesandbox.io/s/qxnmn1n45q
99
This hook allows you to smoothly animate any value using an easing function (linear, elastic, etc). In the example we call the useAnimation hook three times to animated three balls on to the screen at different intervals. Additionally we show how easy it is to compose hooks. Our useAnimation hook doesn't actual make use of useState or useEffect itself, but instead serves as a wrapper around the useAnimationTimer hook. Having the timer logic abstracted out into its own hook gives us better code readability and the ability to use timer logic in other contexts. Be sure to check out the [CodeSandbox Demo](https://codesandbox.io/s/qxnmn1n45q) for this one.
1010

1111
```jsx
12-
import { useState, useEffect } from 'react';
12+
import { useState, useEffect } from "react";
1313

1414
// Usage
15-
function App()
16-
{
15+
function App() {
1716
// Call hook multiple times to get animated values with different start delays
17+
const animation1 = useAnimation("elastic", 600, 0);
18+
const animation2 = useAnimation("elastic", 600, 150);
19+
const animation3 = useAnimation("elastic", 600, 300);
1820

19-
const animation1 = useAnimation('elastic', 600, 0);
20-
const animation2 = useAnimation('elastic',
21-
600, 150);
22-
const animation3 = useAnimation('elastic', 600, 300);
23-
24-
return
25-
(
26-
<div style={{ display: 'flex', justifyContent: 'center' }}>
21+
return (
22+
<div style={{ display: "flex", justifyContent: "center" }}>
2723
<Ball
28-
2924
innerStyle={{
30-
marginTop: animation1 * 200 - 100
25+
marginTop: animation1 * 200 - 100,
3126
}}
32-
3327
/>
3428

3529
<Ball
3630
innerStyle={{
37-
marginTop: animation2
38-
* 200 - 100
31+
marginTop: animation2 * 200 - 100,
3932
}}
4033
/>
4134

4235
<Ball
4336
innerStyle={{
44-
45-
marginTop: animation3 * 200 - 100
37+
marginTop: animation3 * 200 - 100,
4638
}}
4739
/>
4840
</div>
49-
5041
);
5142
}
5243

5344
const Ball = ({ innerStyle }) => (
5445
<div
5546
style={{
56-
5747
width: 100,
5848
height: 100,
59-
marginRight: '40px',
60-
borderRadius:
61-
'50px',
62-
backgroundColor: '#4dd5fa',
63-
...innerStyle
49+
marginRight: "40px",
50+
borderRadius: "50px",
51+
backgroundColor: "#4dd5fa",
52+
...innerStyle,
6453
}}
65-
6654
/>
6755
);
6856

6957
// Hook
70-
function useAnimation(
71-
easingName = 'linear',
72-
73-
duration = 500,
74-
delay = 0
75-
) {
76-
// The useAnimationTimer hook calls
77-
useState every animation frame ...
78-
// ... giving us elapsed time and causing
79-
a rerender as frequently ...
58+
function useAnimation(easingName = "linear", duration = 500, delay = 0) {
59+
// The useAnimationTimer hook calls useState every animation frame ...
60+
// ... giving us elapsed time and causing a rerender as frequently ...
8061
// ... as possible for a smooth animation.
81-
8262
const elapsed = useAnimationTimer(duration, delay);
83-
// Amount of specified
84-
duration elapsed on a scale from 0 - 1
63+
// Amount of specified duration elapsed on a scale from 0 - 1
8564
const n = Math.min(1, elapsed / duration);
86-
8765
// Return altered value based on our specified easing function
8866
return easing[easingName](n);
8967
}
9068

91-
//
92-
Some easing functions copied from:
69+
// Some easing functions copied from:
9370
// https://github.com/streamich/ts-easing/blob/master/src/index.ts
94-
//
95-
Hardcode here or pull in a dependency
71+
// Hardcode here or pull in a dependency
9672
const easing = {
97-
linear: n => n,
98-
99-
elastic: n =>
100-
n * (33 * n * n * n * n - 106 * n * n * n + 126 * n * n -
101-
67 * n + 15),
102-
inExpo: n => Math.pow(2, 10 * (n - 1))
73+
linear: (n) => n,
74+
elastic: (n) =>
75+
n * (33 * n * n * n * n - 106 * n * n * n + 126 * n * n - 67 * n + 15),
76+
inExpo: (n) => Math.pow(2, 10 * (n - 1)),
10377
};
10478

105-
function
106-
useAnimationTimer(duration = 1000, delay = 0) {
107-
const [elapsed, setTime] =
108-
useState(0);
79+
function useAnimationTimer(duration = 1000, delay = 0) {
80+
const [elapsed, setTime] = useState(0);
10981

11082
useEffect(
11183
() => {
112-
let animationFrame, timerStop,
113-
start;
84+
let animationFrame, timerStop, start;
11485

11586
// Function to be executed on each animation frame
116-
function
117-
onFrame() {
87+
function onFrame() {
11888
setTime(Date.now() - start);
11989
loop();
12090
}
12191

122-
12392
// Call onFrame() on next animation frame
12493
function loop() {
125-
animationFrame
126-
= requestAnimationFrame(onFrame);
94+
animationFrame = requestAnimationFrame(onFrame);
12795
}
12896

12997
function onStart() {
130-
13198
// Set a timeout to stop things when duration time elapses
132-
timerStop
133-
= setTimeout(() => {
99+
timerStop = setTimeout(() => {
134100
cancelAnimationFrame(animationFrame);
135-
setTime(Date.now()
136-
- start);
101+
setTime(Date.now() - start);
137102
}, duration);
138103

139104
// Start the loop
140-
start
141-
= Date.now();
105+
start = Date.now();
142106
loop();
143107
}
144108

145-
// Start after specified
146-
delay (defaults to 0)
109+
// Start after specified delay (defaults to 0)
147110
const timerDelay = setTimeout(onStart, delay);
148111

149-
150112
// Clean things up
151113
return () => {
152114
clearTimeout(timerStop);
153-
154115
clearTimeout(timerDelay);
155116
cancelAnimationFrame(animationFrame);
156-
157117
};
158118
},
159-
[duration, delay] // Only re-run effect if duration or
160-
delay changes
119+
[duration, delay] // Only re-run effect if duration or delay changes
161120
);
162121

163122
return elapsed;
164123
}
165-
166124
```

src/pages/useAsync.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ It's generally a good practice to indicate to users the status of any async requ
2323
Rather than litter your components with a bunch of `useState` calls to keep track of the state of an async function, you can use our custom hook which takes an async function as an input and returns the `value`, `error`, and `status` values we need to properly update our UI. Possible values for `status` prop are: "idle", "pending", "success", "error". As you'll see in the code below, our hook allows both immediate execution and delayed execution using the returned `execute` function.
2424

2525
```jsx
26-
import React, { useState, useEffect, useCallback } from 'react';
26+
import React, { useState, useEffect, useCallback } from "react";
2727

2828
// Usage
2929
function App() {
3030
const { execute, status, value, error } = useAsync(myFunction, false);
3131

3232
return (
3333
<div>
34-
{status === 'idle' && <div>Start your journey by clicking a button</div>}
35-
{status === 'success' && <div>{value}</div>}
36-
{status === 'error' && <div>{error}</div>}
37-
<button onClick={execute} disabled={status === 'pending'}>
38-
{status !== 'pending' ? 'Click me' : 'Loading...'}
34+
{status === "idle" && <div>Start your journey by clicking a button</div>}
35+
{status === "success" && <div>{value}</div>}
36+
{status === "error" && <div>{error}</div>}
37+
<button onClick={execute} disabled={status === "pending"}>
38+
{status !== "pending" ? "Click me" : "Loading..."}
3939
</button>
4040
</div>
4141
);
@@ -48,15 +48,15 @@ const myFunction = () => {
4848
setTimeout(() => {
4949
const rnd = Math.random() * 10;
5050
rnd <= 5
51-
? resolve('Submitted successfully uD83DuDE4C')
52-
: reject('Oh no there was an error uD83DuDE1E');
51+
? resolve("Submitted successfully 🙌")
52+
: reject("Oh no there was an error 😞");
5353
}, 2000);
5454
});
5555
};
5656

5757
// Hook
5858
const useAsync = (asyncFunction, immediate = true) => {
59-
const [status, setStatus] = useState('idle');
59+
const [status, setStatus] = useState("idle");
6060
const [value, setValue] = useState(null);
6161
const [error, setError] = useState(null);
6262

@@ -65,18 +65,18 @@ const useAsync = (asyncFunction, immediate = true) => {
6565
// useCallback ensures the below useEffect is not called
6666
// on every render, but only if asyncFunction changes.
6767
const execute = useCallback(() => {
68-
setStatus('pending');
68+
setStatus("pending");
6969
setValue(null);
7070
setError(null);
7171

7272
return asyncFunction()
73-
.then(response => {
73+
.then((response) => {
7474
setValue(response);
75-
setStatus('success');
75+
setStatus("success");
7676
})
77-
.catch(error => {
77+
.catch((error) => {
7878
setError(error);
79-
setStatus('error');
79+
setStatus("error");
8080
});
8181
}, [asyncFunction]);
8282

@@ -94,19 +94,19 @@ const useAsync = (asyncFunction, immediate = true) => {
9494
```
9595

9696
```typescript
97-
import React, { useState, useEffect, useCallback } from 'react';
97+
import React, { useState, useEffect, useCallback } from "react";
9898

9999
// Usage
100100
function App() {
101101
const { execute, status, value, error } = useAsync<string>(myFunction, false);
102102

103103
return (
104104
<div>
105-
{status === 'idle' && <div>Start your journey by clicking a button</div>}
106-
{status === 'success' && <div>{value}</div>}
107-
{status === 'error' && <div>{error}</div>}
108-
<button onClick={execute} disabled={status === 'pending'}>
109-
{status !== 'pending' ? 'Click me' : 'Loading...'}
105+
{status === "idle" && <div>Start your journey by clicking a button</div>}
106+
{status === "success" && <div>{value}</div>}
107+
{status === "error" && <div>{error}</div>}
108+
<button onClick={execute} disabled={status === "pending"}>
109+
{status !== "pending" ? "Click me" : "Loading..."}
110110
</button>
111111
</div>
112112
);
@@ -119,8 +119,8 @@ const myFunction = (): Promise<string> => {
119119
setTimeout(() => {
120120
const rnd = Math.random() * 10;
121121
rnd <= 5
122-
? resolve('Submitted successfully 🙌')
123-
: reject('Oh no there was an error 😞');
122+
? resolve("Submitted successfully 🙌")
123+
: reject("Oh no there was an error 😞");
124124
}, 2000);
125125
});
126126
};
@@ -131,8 +131,8 @@ const useAsync = <T, E = string>(
131131
immediate = true
132132
) => {
133133
const [status, setStatus] = useState<
134-
'idle' | 'pending' | 'success' | 'error'
135-
>('idle');
134+
"idle" | "pending" | "success" | "error"
135+
>("idle");
136136
const [value, setValue] = useState<T | null>(null);
137137
const [error, setError] = useState<E | null>(null);
138138

@@ -141,18 +141,18 @@ const useAsync = <T, E = string>(
141141
// useCallback ensures the below useEffect is not called
142142
// on every render, but only if asyncFunction changes.
143143
const execute = useCallback(() => {
144-
setStatus('pending');
144+
setStatus("pending");
145145
setValue(null);
146146
setError(null);
147147

148148
return asyncFunction()
149149
.then((response: any) => {
150150
setValue(response);
151-
setStatus('success');
151+
setStatus("success");
152152
})
153153
.catch((error: any) => {
154154
setError(error);
155-
setStatus('error');
155+
setStatus("error");
156156
});
157157
}, [asyncFunction]);
158158

src/pages/useAuth.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ firebase.initializeApp({
6464
apiKey: "",
6565
authDomain: "",
6666
projectId: "",
67-
appID: ""
67+
appID: "",
6868
});
6969

7070
const authContext = createContext();
@@ -92,7 +92,7 @@ function useProvideAuth() {
9292
return firebase
9393
.auth()
9494
.signInWithEmailAndPassword(email, password)
95-
.then(response => {
95+
.then((response) => {
9696
setUser(response.user);
9797
return response.user;
9898
});
@@ -102,7 +102,7 @@ function useProvideAuth() {
102102
return firebase
103103
.auth()
104104
.createUserWithEmailAndPassword(email, password)
105-
.then(response => {
105+
.then((response) => {
106106
setUser(response.user);
107107
return response.user;
108108
});
@@ -117,7 +117,7 @@ function useProvideAuth() {
117117
});
118118
};
119119

120-
const sendPasswordResetEmail = email => {
120+
const sendPasswordResetEmail = (email) => {
121121
return firebase
122122
.auth()
123123
.sendPasswordResetEmail(email)
@@ -140,7 +140,7 @@ function useProvideAuth() {
140140
// ... component that utilizes this hook to re-render with the ...
141141
// ... latest auth object.
142142
useEffect(() => {
143-
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
143+
const unsubscribe = firebase.auth().onAuthStateChanged((user) => {
144144
if (user) {
145145
setUser(user);
146146
} else {
@@ -159,7 +159,7 @@ function useProvideAuth() {
159159
signup,
160160
signout,
161161
sendPasswordResetEmail,
162-
confirmPasswordReset
162+
confirmPasswordReset,
163163
};
164164
}
165165
```

0 commit comments

Comments
 (0)