|
| 1 | +import React, {useEffect, useRef, useState} from "react"; |
| 2 | + |
| 3 | +// 根据初始值自动推断 |
| 4 | +// 场景:明确的初始值 |
| 5 | + |
| 6 | +type User = { |
| 7 | + name: string |
| 8 | + age: number |
| 9 | +} |
| 10 | + |
| 11 | +// props 注解 |
| 12 | +// type Props = { |
| 13 | +// className: string |
| 14 | +// } |
| 15 | +interface Props { |
| 16 | + className: string |
| 17 | +} |
| 18 | + |
| 19 | +function Button(props: Props) { |
| 20 | + const {className} = props |
| 21 | + return <button className={className}>Click me</button> |
| 22 | +} |
| 23 | + |
| 24 | +// props & typescript - 为 children 添加类型 |
| 25 | +type Props2 = { |
| 26 | + className: string |
| 27 | + children: React.ReactNode |
| 28 | +} |
| 29 | + |
| 30 | +function Button2(props: Props2) { |
| 31 | + const {className, children} = props |
| 32 | + return <button className={className}>{children}</button> |
| 33 | +} |
| 34 | + |
| 35 | +// props & typescript - 为事件 prop 添加类型 |
| 36 | +type Props3 = { |
| 37 | + onGetMsg?: (msg: string) => void |
| 38 | +} |
| 39 | + |
| 40 | +function Son(props: Props3) { |
| 41 | + const {onGetMsg} = props |
| 42 | + const clickHandler = () => { |
| 43 | + onGetMsg?.('this is msg') |
| 44 | + } |
| 45 | + return <button onClick={clickHandler}>sendMsg</button> |
| 46 | +} |
| 47 | + |
| 48 | +function App() { |
| 49 | + const [value, setValue] = useState(false) |
| 50 | + |
| 51 | + const [list, setList] = useState([1, 2, 3]) |
| 52 | + const changeValue = () => { |
| 53 | + setValue(true) |
| 54 | + } |
| 55 | + |
| 56 | + const changeList = () => { |
| 57 | + setList([4, 5, 6]) |
| 58 | + } |
| 59 | + |
| 60 | + // 限制初始值的类型 |
| 61 | + // const [user, setUser] = useState<User>({ |
| 62 | + // name: 'jack', |
| 63 | + // age: 18 |
| 64 | + // }) |
| 65 | + |
| 66 | + // const [user, setUser] = useState<User>(() => { |
| 67 | + // return { |
| 68 | + // name: 'jack', |
| 69 | + // age: 18 |
| 70 | + // } |
| 71 | + // }) |
| 72 | + |
| 73 | + const [user, setUser] = useState<User>({ |
| 74 | + name: 'jack', |
| 75 | + age: 18 |
| 76 | + }) |
| 77 | + |
| 78 | + // const changeUser = () => { |
| 79 | + // setUser({ |
| 80 | + // name: 'john', |
| 81 | + // age: 28 |
| 82 | + // }) |
| 83 | + // } |
| 84 | + const changeUser = () => { |
| 85 | + setUser(() => ({ |
| 86 | + name: 'john', |
| 87 | + age: 28 |
| 88 | + })) |
| 89 | + } |
| 90 | + |
| 91 | + const [user2, setUser2] = useState<User | null>(null) |
| 92 | + |
| 93 | + const changeUser2 = () => { |
| 94 | + setUser2(null) |
| 95 | + setUser2({ |
| 96 | + name: 'jack', |
| 97 | + age: 18 |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + // useRef & typescript |
| 102 | + const domRef = useRef<HTMLInputElement>(null); |
| 103 | + useEffect(() => { |
| 104 | + domRef.current?.focus() |
| 105 | + }, []) |
| 106 | + |
| 107 | + |
| 108 | + return ( |
| 109 | + <> |
| 110 | + this is App{value}{list} |
| 111 | + <button onClick={changeValue}>1</button> |
| 112 | + <button onClick={changeList}>2</button> |
| 113 | + <br /> |
| 114 | + |
| 115 | + <button onClick={changeUser}>3</button> |
| 116 | + {user.name} |
| 117 | + <br /> |
| 118 | + |
| 119 | + {/* 为了类型安全, 可选链做类型守卫 */} |
| 120 | + {/* 只有 user 不为 null 时,才做点运算*/} |
| 121 | + {user2?.age} |
| 122 | + <button onClick={changeUser2}>4</button> |
| 123 | + <br /> |
| 124 | + |
| 125 | + <Button className="test"/> |
| 126 | + <br /> |
| 127 | + |
| 128 | + <Button2 className="test">children</Button2> |
| 129 | + <Button2 className="test"> |
| 130 | + <span>this is span</span> |
| 131 | + </Button2> |
| 132 | + <br /> |
| 133 | + |
| 134 | + <Son onGetMsg={(msg) => console.log(msg)} /> |
| 135 | + <br /> |
| 136 | + |
| 137 | + <input type="text" ref={domRef}/> |
| 138 | + <br /> |
| 139 | + </> |
| 140 | + ) |
| 141 | +} |
| 142 | + |
| 143 | +export default App |
0 commit comments