Skip to content

Commit c1898ed

Browse files
authored
Merge pull request #2 from atomic-state/enh-hooks-components
enh(WithLayout):
2 parents a00c759 + e3e3ad0 commit c1898ed

File tree

4 files changed

+43
-49
lines changed

4 files changed

+43
-49
lines changed

README.md

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ Usage
2929
import { useBoolean } from 'react-kuh'
3030

3131
function App(){
32-
const [active, setActive] = useBoolean(false) // if not present, default is null
32+
const [active, actions] = useBoolean(false) // if not present, default is null
3333

3434
return (
3535
<div>
36-
<button onClick={setActive.on}>Active</button>
37-
<button onClick={setActive.off}>Inactive</button>
38-
<button onClick={setActive.off}>Inactive</button>
39-
<button onClick={() => setActive.set(false)}>Set</button>
40-
<button onClick={setActive.reset}>Reset</button>
36+
<button onClick={actions.on}>Active</button>
37+
<button onClick={actions.off}>Inactive</button>
38+
<button onClick={actions.off}>Inactive</button>
39+
<button onClick={() => actions.set(false)}>Set</button>
40+
<button onClick={actions.reset}>Reset</button>
4141
</div>
4242
)
4343
}
@@ -57,13 +57,13 @@ type NoteType = {
5757

5858
function App() {
5959
// Type is not required if it should be inferred from the default value
60-
const [note, setNote] = useObject<NoteType>({
60+
const [note, actions] = useObject<NoteType>({
6161
title: '',
6262
content: ''
6363
})
6464

6565
function randomizeNote() {
66-
setNote.replace({
66+
actions.setValue({
6767
title: Math.random().toString(12).split('.')[1],
6868
content: Math.random().toString(12).split('.')[1]
6969
})
@@ -74,20 +74,20 @@ function App() {
7474
<input
7575
value={note.title}
7676
onChange={e => {
77-
setNote.write({
77+
actions.setPartialValue({
7878
title: e.target.value
7979
})
8080
}}
8181
/>
8282
<input
8383
value={note.content}
8484
onChange={e => {
85-
setNote.write({
85+
actions.setPartialValue({
8686
content: e.target.value
8787
})
8888
}}
8989
/>
90-
<button onClick={setNote.reset}>Reset to initial value</button>
90+
<button onClick={actions.reset}>Reset to initial value</button>
9191
<button onClick={randomizeNote}>Random</button>
9292
</div>
9393
)
@@ -107,7 +107,7 @@ This component renders its children after the first render. This can be used as
107107
Usage
108108

109109
```jsx
110-
import { BrowserOnly } = from 'react-kuh'
110+
import { BrowserOnly } from 'react-kuh'
111111

112112
export default function Page(){
113113
return (
@@ -119,26 +119,4 @@ export default function Page(){
119119
</div>
120120
)
121121
}
122-
```
123-
124-
#### `ClientOnly` (component)
125-
126-
This component renders its children as they are passed to it, this component has the `use client` directive at the top so it can be used in Next.js's server components to wrap client-only components as well as server components.
127-
128-
Usage
129-
130-
```jsx
131-
import { ClientOnly } = from 'react-kuh'
132-
133-
export default function Page(){
134-
return (
135-
<div>
136-
<SomeServerComponent/>
137-
<BrowserOnly>
138-
<AnotherServerComponent/>
139-
<SomeClientComponent/>
140-
</BrowserOnly>
141-
</div>
142-
)
143-
}
144122
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-kuh",
3-
"version": "0.0.9",
3+
"version": "0.1.0",
44
"description": "Some React kinda useful hooks and components",
55
"main": "dist/index.js",
66
"keywords": [],

src/components/index.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,36 @@ function SSRSuspense({ fallback, children }: any) {
4444
return ssr ? fallback : <Suspense fallback={fallback}>{children}</Suspense>
4545
}
4646

47-
export default function WithLayout({
47+
export function WithLayout<L>({
4848
Component,
4949
pageProps,
5050
layoutProps,
5151
loadingProps,
52-
}: any) {
53-
const Layout = Component.Layout || (({ children }: any) => children)
54-
const Loading = Component.Loading || (() => null)
52+
defaultLayout = ({ children }: any) => children,
53+
defaultLoading = () => null,
54+
showLayout = true,
55+
showLoading = true,
56+
}: {
57+
Component?: any
58+
pageProps?: any
59+
layoutProps?: any
60+
loadingProps?: any
61+
defaultLayout?: any
62+
defaultLoading?: any
63+
showLayout?: boolean
64+
showLoading?: boolean
65+
}) {
66+
const Layout = Component.Layout || defaultLayout
67+
const Loading = Component.Loading || defaultLoading
68+
69+
const SLayout = showLayout ? Layout : ({ children }: any) => children
70+
const SLoading = showLoading ? Loading : () => null
71+
5572
return (
56-
<Layout {...{ ...layoutProps, Component }}>
57-
<SSRSuspense fallback={<Loading {...{ ...loadingProps, Component }} />}>
73+
<SLayout {...{ ...layoutProps, Component }}>
74+
<SSRSuspense fallback={<SLoading {...{ ...loadingProps, Component }} />}>
5875
<Component {...pageProps} />
5976
</SSRSuspense>
60-
</Layout>
77+
</SLayout>
6178
)
6279
}

src/hooks/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@ export function useBoolean(initialValue: boolean | null = null as any) {
5959
on() {
6060
setState(true)
6161
},
62+
/**
63+
* @deprecated Use `setValue` instead
64+
*/
6265
set(v: boolean) {
6366
setState(v)
6467
},
68+
setValue(v: boolean) {
69+
setState(v)
70+
},
6571
reset() {
6672
setState(initialValue as boolean)
6773
},
@@ -121,13 +127,6 @@ export function useObject<T = any>(initialValue: T) {
121127
return end
122128
}
123129

124-
function useE() {
125-
const [state, actions] = useObject({
126-
name: "dany",
127-
email: "",
128-
})
129-
}
130-
131130
/**
132131
* Returns `true` after the component mounts/hydrates (after the first render)
133132
*/

0 commit comments

Comments
 (0)