Skip to content

Commit 53a1470

Browse files
committed
[WIP] Show component
1 parent 5175cbf commit 53a1470

6 files changed

Lines changed: 60 additions & 11 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#
2+
3+
4+
## Usage
5+
6+
```tsx
7+
8+
export default function ShowComponent() {
9+
const [c, setC] = useState(0);
10+
useEffect(() => {
11+
const interval = setInterval(() => setC(c => c + 1), 1000);
12+
return () => clearInterval(interval);
13+
}, []);
14+
return (
15+
<div>
16+
<h1>Hello StackBlitz!</h1>
17+
<p>Start editing to see some magic happen :)</p>
18+
<Show when={c % 2 === 0} fallback={"loading..."}>
19+
<p>Done</p>
20+
</Show>
21+
</div>
22+
);
23+
}
24+
```
25+
26+
27+
28+
29+
## API
30+
31+
```tsx
32+
Show = ({ when, fallback, children }: PropsWithChildren<{ when: boolean, fallback?: ReactNode }>)
33+
```
34+
35+
> ### Params
36+
>
37+
>
38+
>
39+
40+
> ### Returns
41+
>
42+
>
43+
>
44+
>

apps/react-tools-demo/src/markdown/createPubSubStore.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const store = createPubSubStore(
1212
name: "",
1313
eta: 0
1414
},
15-
counter: 0,
1615
spinner: false
1716
},
1817
{
@@ -26,9 +25,6 @@ export const { usePubSubStore, mutateStore } = store;
2625
//import {usePubSubStore} from '../store.ts';
2726
const Comp1 = () => {
2827
const [state, setState] = usePubSubStore(store => store.user);
29-
useEffect(() => {
30-
console.log(state);
31-
}, [state])
3228

3329
return <div>
3430
<label htmlFor="id">ID:</label>
@@ -58,15 +54,9 @@ const Comp2 = memo(() => {
5854
//import {usePubSubStore} from '../store.ts';
5955
export const CreatePubSubStore = () => {
6056
const [spinner] = usePubSubStore(store => store.spinner);
61-
const [counter, setCounter] = usePubSubStore(store => store.counter);
62-
useEffect(() => {
63-
const id = setInterval(() => setCounter(counter => counter++), 1000);
64-
return () => clearInterval(id);
65-
}, [setCounter])
6657
return <div style={{ display: "grid", gridTemplateRows: "auto auto", gap: 20, justifyContent: "center" }}>
6758
<fieldset style={{padding: 20}}>
6859
<legend>Component 2</legend>
69-
<p>{counter}</p>
7060
<Comp2 />
7161
</fieldset>
7262
{

packages/react-tools/src/components/LazyComponent.tsx renamed to packages/react-tools/src/components/Lazy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const DynamicComponent = ({ factory, beforeLoad, afterLoad }: { factory: () => P
3232
throw current.promise;
3333
};
3434

35-
export const LazyComponent = <T extends ComponentType<unknown>>({ factory, fallback, beforeLoad, afterLoad }: { factory: () => Promise<T | { default: T }>, fallback?: ReactNode, beforeLoad?: ()=>void, afterLoad?: ()=>void }) => {
35+
export const Lazy = <T extends ComponentType<unknown>>({ factory, fallback, beforeLoad, afterLoad }: { factory: () => Promise<T | { default: T }>, fallback?: ReactNode, beforeLoad?: ()=>void, afterLoad?: ()=>void }) => {
3636
return <Suspense fallback={fallback}>
3737
<DynamicComponent factory={factory} beforeLoad={beforeLoad} afterLoad={afterLoad} />
3838
</Suspense>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { PropsWithChildren, ReactNode } from "react";
2+
3+
export const Show = ({ when, fallback, children }: PropsWithChildren<{ when: boolean, fallback?: ReactNode }>) => {
4+
if (!when) {
5+
return fallback ? fallback : null;
6+
}
7+
return children;
8+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { Lazy } from './Lazy';
2+
export { Show } from './Show';

packages/react-tools/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ export {
206206
createPubSubStore
207207
} from './hooks'
208208

209+
export {
210+
Show,
211+
Lazy
212+
} from './components'
213+
209214
export {
210215
isShallowEqual,
211216
isDeepEqual,

0 commit comments

Comments
 (0)