File tree Expand file tree Collapse file tree 5 files changed +48
-2
lines changed
2024 Prep/machine_coding/react/src Expand file tree Collapse file tree 5 files changed +48
-2
lines changed Original file line number Diff line number Diff line change 1
1
import './App.css'
2
- import { LiftingUpState } from './components'
2
+ import { InputHookComp } from './components'
3
3
4
4
function App ( ) {
5
5
return (
6
6
< div className = "container" >
7
7
< h1 > Vite + React</ h1 >
8
- < LiftingUpState />
8
+ < InputHookComp />
9
9
</ div >
10
10
)
11
11
}
Original file line number Diff line number Diff line change
1
+ import { useInput } from '../hooks'
2
+
3
+ function InputHookComp ( ) {
4
+ const input = useInput ( {
5
+ initialValue : '' ,
6
+ } )
7
+
8
+ return (
9
+ < >
10
+ < input
11
+ type = "text"
12
+ value = { input . value }
13
+ onChange = { input . handleChange }
14
+ placeholder = "Enter your name here"
15
+ />
16
+ < h4 > value: { input . value } </ h4 >
17
+ < button onClick = { input . reset } > Reset</ button >
18
+ </ >
19
+ )
20
+ }
21
+
22
+ export default InputHookComp
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import FileExplorer from './FileExplorer'
4
4
import BatchingStates from './BatchingStates'
5
5
import CountDownTimer from './Timer'
6
6
import LiftingUpState from './LiftingStateUp'
7
+ import InputHookComp from './InputCustomHookComp'
7
8
8
9
export {
9
10
Loader ,
@@ -12,4 +13,5 @@ export {
12
13
BatchingStates ,
13
14
CountDownTimer ,
14
15
LiftingUpState ,
16
+ InputHookComp ,
15
17
}
Original file line number Diff line number Diff line change 1
1
export { useFetch } from './useFetchAsync'
2
2
export { useDebounce } from './useDebounce'
3
+ export { useInput } from './useInput'
Original file line number Diff line number Diff line change
1
+ import { useState } from 'react'
2
+
3
+ export interface IUseInputProps {
4
+ initialValue : string | undefined
5
+ }
6
+ const useInput = ( props : IUseInputProps ) => {
7
+ const { initialValue = '' } = props
8
+
9
+ const [ value , setValue ] = useState < string > ( initialValue )
10
+
11
+ const handleChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
12
+ setValue ( event . target . value )
13
+ }
14
+
15
+ const reset = ( ) => {
16
+ setValue ( initialValue )
17
+ }
18
+ return { value, handleChange, reset }
19
+ }
20
+
21
+ export { useInput }
You can’t perform that action at this time.
0 commit comments