Skip to content

Commit

Permalink
feat: create 'use'
Browse files Browse the repository at this point in the history
  • Loading branch information
DoK6n committed Sep 29, 2024
1 parent 1d82e6c commit 333fc36
Show file tree
Hide file tree
Showing 26 changed files with 561 additions and 148 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default [
},
],
'react-hooks/exhaustive-deps': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
},
]
3 changes: 2 additions & 1 deletion examples/vite-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1"
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.1"
},
"devDependencies": {
"@types/react": "^18.3.3",
Expand Down
34 changes: 34 additions & 0 deletions examples/vite-ts/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 0 additions & 42 deletions examples/vite-ts/src/App.css

This file was deleted.

68 changes: 0 additions & 68 deletions examples/vite-ts/src/index.css

This file was deleted.

16 changes: 16 additions & 0 deletions examples/vite-ts/src/lib/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface Post {
id: number
title: string
body: string
tags: string[]
reactions: {
likes: number
dislikes: number
}
views: number
userId: number
}

export interface ErrorResponse {
message: string
}
5 changes: 5 additions & 0 deletions examples/vite-ts/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CSSProperties } from 'react'

export const styler = <T extends string>(
styles: Record<T, CSSProperties>,
): Record<T, CSSProperties> => styles
6 changes: 3 additions & 3 deletions examples/vite-ts/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './index.css'
import { RouterProvider } from 'react-router-dom'
import { router } from './router'

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
<RouterProvider router={router} />
</React.StrictMode>,
)
63 changes: 63 additions & 0 deletions examples/vite-ts/src/pages/RootLayout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Link, Outlet, useNavigate } from 'react-router-dom'
import { styler } from '../lib/utils'

const createRandomPostId = () => Math.floor(Math.random() * 251) + 1

export default function RootLayout() {
const navigate = useNavigate()

return (
<main style={styles.main}>
<nav style={styles.nav}>
<Link to='/lifecycle' style={styles.link}>
life cycle hook
</Link>
<button
onClick={() => {
navigate(`/message/${createRandomPostId()}`)
}}
style={{ ...styles.button, ...styles.link }}
>
use hook
</button>
</nav>

<div style={styles.outletWrapper}>
<Outlet />
</div>
</main>
)
}

const styles = styler({
main: {
display: 'flex',
flexDirection: 'column',
minHeight: '100vh',
},
nav: {
backgroundColor: '#f0f0f0',
padding: '1rem',
display: 'flex',
justifyContent: 'space-around',
},
link: {
textDecoration: 'none',
color: '#333',
fontWeight: 'bold',
},
outletWrapper: {
flex: 1,
padding: '2rem',
backgroundColor: '#ffffff',
},
button: {
border: 'none',
background: 'none',
padding: 0,
margin: 0,
font: 'inherit',
cursor: 'pointer',
outline: 'none',
},
})
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import { useReducer, useState } from 'react'
import './App.css'
import {
useMonitoringState,
useMount,
useMountBeforePaint,
useUpdateBeforePaint,
useUnmount,
useUnmountBeforePaint,
useMount,
useUpdate,
useUnmount,
useMonitoringState,
} from '../../../dist'

function Child() {
useMountBeforePaint(() => {
console.log('Child mounted before paint')
})

useUnmountBeforePaint(() => {
console.log('Child unmounted before paint')
})

useMount(() => {
console.log('Child mounted')
})

useUnmount(() => {
console.log('Child unmounted')
})
useUpdateBeforePaint,
} from '../../../../../dist'
import Child from './ui/Child'
import { RouteObject } from 'react-router-dom'

return <div className='card'>Child</div>
export const LifeCycleRoute: RouteObject = {
path: '/lifecycle',
element: <LifeCyclePage />,
}

function App() {
export default function LifeCyclePage() {
const [count, setCount] = useState(0)
const [isShow, toggle] = useReducer(prev => !prev, true)
const [isShow, toggle] = useReducer(state => !state, true)

useMountBeforePaint(() => {
console.log('Before paint')
Expand Down Expand Up @@ -74,5 +60,3 @@ function App() {
</div>
)
}

export default App
26 changes: 26 additions & 0 deletions examples/vite-ts/src/pages/lifeCycle/ui/Child.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
useMount,
useMountBeforePaint,
useUnmount,
useUnmountBeforePaint,
} from '../../../../../../dist'

export default function Child() {
useMountBeforePaint(() => {
console.log('Child mounted before paint')
})

useUnmountBeforePaint(() => {
console.log('Child unmounted before paint')
})

useMount(() => {
console.log('Child mounted')
})

useUnmount(() => {
console.log('Child unmounted')
})

return <div className='card'>Child</div>
}
15 changes: 15 additions & 0 deletions examples/vite-ts/src/pages/message/MessagePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { GetMessage } from './ui'
import { RouteObject, useParams } from 'react-router-dom'
import { getMessage } from './lib'

export const MessageRoute: RouteObject = {
path: '/message/:id',
element: <MessagePage />,
}

function MessagePage() {
const { id } = useParams()

return <GetMessage promise={getMessage(+id)} />
}
export default MessagePage
Loading

0 comments on commit 333fc36

Please sign in to comment.