Skip to content

Commit a00c759

Browse files
authored
Merge pull request #1 from atomic-state/enh-hooks-components
Enh hooks components
2 parents 9859d80 + 9ff9ea5 commit a00c759

File tree

4 files changed

+69
-26
lines changed

4 files changed

+69
-26
lines changed

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.8",
3+
"version": "0.0.9",
44
"description": "Some React kinda useful hooks and components",
55
"main": "dist/index.js",
66
"keywords": [],

src/components/index.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use client"
2-
import React, { useState, useEffect } from "react"
2+
import React, { Suspense, useState, useEffect } from "react"
33

44
let isServer: boolean
55

@@ -25,3 +25,38 @@ export function BrowserOnly({ children }: { children?: React.ReactNode }) {
2525
export function ClientOnly({ children }: { children: React.ReactNode }) {
2626
return children as JSX.Element
2727
}
28+
29+
/**
30+
* For Next.js pages router
31+
*/
32+
33+
let isServer__layout = true
34+
35+
function SSRSuspense({ fallback, children }: any) {
36+
const [ssr, setSSR] = useState(isServer__layout)
37+
38+
useEffect(() => {
39+
setSSR(false)
40+
isServer__layout = false
41+
}, [])
42+
43+
// This will render the fallback in the server
44+
return ssr ? fallback : <Suspense fallback={fallback}>{children}</Suspense>
45+
}
46+
47+
export default function WithLayout({
48+
Component,
49+
pageProps,
50+
layoutProps,
51+
loadingProps,
52+
}: any) {
53+
const Layout = Component.Layout || (({ children }: any) => children)
54+
const Loading = Component.Loading || (() => null)
55+
return (
56+
<Layout {...{ ...layoutProps, Component }}>
57+
<SSRSuspense fallback={<Loading {...{ ...loadingProps, Component }} />}>
58+
<Component {...pageProps} />
59+
</SSRSuspense>
60+
</Layout>
61+
)
62+
}

src/hooks/index.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function useWindowSize() {
4747
}
4848

4949
export function useBoolean(initialValue: boolean | null = null as any) {
50-
const [state, setState] = useState<boolean>(initialValue)
50+
const [state, setState] = useState<boolean>(initialValue as boolean)
5151

5252
const actions = {
5353
toggle() {
@@ -63,20 +63,11 @@ export function useBoolean(initialValue: boolean | null = null as any) {
6363
setState(v)
6464
},
6565
reset() {
66-
setState(initialValue)
66+
setState(initialValue as boolean)
6767
},
6868
}
6969

70-
const end = [state, actions] as [
71-
boolean,
72-
{
73-
toggle(): void
74-
off(): void
75-
on(): void
76-
set(v: boolean): void
77-
reset(): void
78-
}
79-
]
70+
const end = [state, actions] as const
8071

8172
return end
8273
}
@@ -85,6 +76,9 @@ export function useObject<T = any>(initialValue: T) {
8576
const [state, setState] = useState<T>(initialValue)
8677

8778
const actions = {
79+
/**
80+
* @deprecated Use `setPartialValue` instead
81+
*/
8882
write(f: Partial<T> | ((e: T) => Partial<T>)) {
8983
const n = (
9084
typeof f === "function" ? (f as any)(state) : { ...state, ...f }
@@ -95,31 +89,45 @@ export function useObject<T = any>(initialValue: T) {
9589
...n,
9690
}))
9791
},
92+
/**
93+
* @deprecated Use `setValue` instead
94+
*/
9895
replace(f: T | ((e: T) => T)) {
9996
const n = typeof f === "function" ? (f as any)(state) : f
10097
setState(n)
10198
},
10299

100+
setPartialValue(f: Partial<T> | ((e: T) => Partial<T>)) {
101+
const n = (
102+
typeof f === "function" ? (f as any)(state) : { ...state, ...f }
103+
) as T
104+
105+
setState((s) => ({
106+
...s,
107+
...n,
108+
}))
109+
},
110+
setValue(f: T | ((e: T) => T)) {
111+
const n = typeof f === "function" ? (f as any)(state) : f
112+
setState(n)
113+
},
103114
reset() {
104115
setState(initialValue)
105116
},
106117
}
107118

108-
const end = [state, actions] as [
109-
T,
110-
{
111-
write(f: Partial<T> | ((e: T) => Partial<T>)): void
112-
replace(f: T | ((e: T) => T)): void
113-
/**
114-
* Reset to initial value
115-
*/
116-
reset(): void
117-
}
118-
]
119+
const end = [state, actions, setState] as const
119120

120121
return end
121122
}
122123

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

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"use client"
22
export { useBoolean, useObject, useSecondRender, useWindowSize } from "./hooks"
3-
export { BrowserOnly, ClientOnly } from "./components"
3+
export { BrowserOnly, ClientOnly, WithLayout } from "./components"

0 commit comments

Comments
 (0)