1773239962

This commit is contained in:
Docker7530
2026-03-11 22:39:26 +08:00
parent cef89547a6
commit a85ad8447f
13 changed files with 680 additions and 142 deletions
@@ -144,7 +144,7 @@ export default FunctionalCounter
# 第二章 组件化开发核心
Props 通信
Props 通信(只读的,子组件不可以进行修改,单向数据流。)
```tsx
type Props = {
@@ -163,12 +163,59 @@ export default PrintNmae
```
传参
普通传参
```tsx
<PrintNmae name="John" />
```
children 传参,这里 type
```tsx
import React from 'react'
type CardProps = React.PropsWithChildren<{}>
const Card = ({ children }: CardProps) => {
return (
<div
style={{ border: '1px solid #ccc', padding: '16px', borderRadius: '8px' }}
>
{children}
</div>
)
}
export default Card
```
关于 `{ children }: CardProps` 是组件接收一个 props 然后从里边结构出来 children
类型声明:
```tsx
type UserProps = {
name: string
age: number
isVerified: boolean
hobbies?: string[]
}
export const User = (user: UserProps) => {
const { name, age, isVerified, hobbies } = user
return (
<div>
<h2>{name}</h2>
<p>Age: {age}</p>
<p>: {isVerified ? '已验证' : '未验证'}</p>
{hobbies && hobbies.length > 0 && <p>: {hobbies.join(', ')}</p>}
</div>
)
}
```
# CSS
顶级作用域