reaxes-toolkit 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +6 -1
- package/package.json +1 -1
- package/readme.md +70 -74
package/changelog.md
CHANGED
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -19,41 +19,41 @@ $ npm i -S reaxes-toolkit
|
|
|
19
19
|
|
|
20
20
|
### 🛠 在 `reaxel` 中使用
|
|
21
21
|
```tsx
|
|
22
|
-
import { rexaStatus } from 'reaxes-toolkit';
|
|
23
22
|
//reaxels/user-bio.ts
|
|
23
|
+
import { rexaStatus } from 'reaxes-toolkit';
|
|
24
24
|
export const reaxel_UserBio = reaxel(() => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
25
|
+
const { status,setStatus } = rexaStatus();
|
|
26
|
+
const { store , setState , mutate } = createReaxable({
|
|
27
|
+
bio : null as string ,
|
|
28
|
+
status ,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const fetchBio = () => {
|
|
32
|
+
if( store.status.pending ) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
new Promise<string>(( resolve , reject ) => {
|
|
36
|
+
setStatus({pending : true});
|
|
37
|
+
setTimeout(() => {
|
|
38
|
+
//Choose which code to comment out to switch different promise results.
|
|
39
|
+
resolve(`I'm an software engineer.`);
|
|
40
|
+
// reject('403 Forbidden');
|
|
41
|
+
} , 2000);
|
|
42
|
+
}).
|
|
43
|
+
then(( bio ) => {
|
|
44
|
+
setState({ bio });
|
|
45
|
+
setStatus({ pending : false , error : false });
|
|
46
|
+
}).catch(e => {
|
|
47
|
+
setStatus({error : e,pending : false})
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
const rtn = {
|
|
51
|
+
UserBio_Store : store ,
|
|
52
|
+
UserBio_SetState : setState ,
|
|
53
|
+
UserBio_Mutate : mutate ,
|
|
54
|
+
fetchBio ,
|
|
55
|
+
};
|
|
56
|
+
return () => rtn;
|
|
57
57
|
});
|
|
58
58
|
|
|
59
59
|
//components/UserBio.tsx
|
|
@@ -61,20 +61,20 @@ import { useEffect } from 'react';
|
|
|
61
61
|
import { reaxel_UserBio } from '../reaxels/user-bio';
|
|
62
62
|
|
|
63
63
|
export default reaxper(() => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
64
|
+
const {fetchBio,UserBio_Store} = reaxel_UserBio();
|
|
65
|
+
|
|
66
|
+
React.useEffect(() => {
|
|
67
|
+
fetchBio();
|
|
68
|
+
} , []);
|
|
69
|
+
|
|
70
|
+
if(UserBio_Store.status.error){
|
|
71
|
+
return <div>Error Code: {UserBio_Store.status.error}</div>
|
|
72
|
+
}
|
|
73
|
+
if(UserBio_Store.status.pending){
|
|
74
|
+
return <div>pending...</div>
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return <div>{ UserBio_Store.bio }</div>;
|
|
78
78
|
});
|
|
79
79
|
```
|
|
80
80
|
|
|
@@ -83,33 +83,29 @@ export default reaxper(() => {
|
|
|
83
83
|
### 🏗 纯 `React` 示例
|
|
84
84
|
```tsx
|
|
85
85
|
const Test = reaxper(() => {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return <div>Error!</div>;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return <div>{ data }</div>;
|
|
86
|
+
const { current : { status,setStatus } } = useRef(rexaStatus());
|
|
87
|
+
const [ data , setData ] = useState(null);
|
|
88
|
+
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
fetch(`api.com`).
|
|
91
|
+
then(( data ) => {
|
|
92
|
+
setData(data);
|
|
93
|
+
setStatus({pending:false});
|
|
94
|
+
}).
|
|
95
|
+
catch(e => {
|
|
96
|
+
setStatus({pending:false,error:e});
|
|
97
|
+
});
|
|
98
|
+
},[]);
|
|
99
|
+
|
|
100
|
+
if( status.pending ) {
|
|
101
|
+
return <div>pending...</div>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if( status.error ) {
|
|
105
|
+
return <div>Error!</div>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return <div>{ data }</div>;
|
|
113
109
|
});
|
|
114
110
|
|
|
115
111
|
import { rexaStatus } from 'reaxes-toolkit';
|