当前位置: 首页 > news >正文

如何选择网站托管公司线上宣传的方式

如何选择网站托管公司,线上宣传的方式,定制开发小程序和模板小程序,购物网站静态页面TypeScript 为 React 开发带来了强大的类型安全保障,这里解析常见的一些TS写法: 一、组件基础类型 1. 函数组件定义 // 显式声明 Props 类型并标注返回值 interface WelcomeProps {name: string;age?: number; // 可选属性 }const Welcome: React.FC…

TypeScript 为 React 开发带来了强大的类型安全保障,这里解析常见的一些TS写法:

一、组件基础类型

1. 函数组件定义

// 显式声明 Props 类型并标注返回值
interface WelcomeProps {name: string;age?: number; // 可选属性
}const Welcome: React.FC<WelcomeProps> = ({ name, age = 18 }) => (<div>Hello {name}, 年龄 {age}</div>
);// React 18+ 需要显式声明 children(如果用到)
interface CardProps {children: React.ReactNode; 
}const Card: React.FC<CardProps> = ({ children }) => (<div className="card">{children}</div>
)

2. 类组件写法

type CounterState = { count: number };class Counter extends React.Component<{}, CounterState> {state: CounterState = { count: 0 };handleClick = () => {this.setState(prev => ({ count: prev.count + 1 }));};render() {return <button onClick={this.handleClick}>点击次数:{this.state.count}</button>;}
}

二、Props 高级用法

1. 联合类型与类型守卫

type User = { id: number;type: 'admin' | 'user';email?: string;accessLevel?: number;
};const UserProfile = ({ user }: { user: User }) => {if (user.type === 'admin') {return <div>管理员权限:{user.accessLevel}</div>;}return <div>用户邮箱:{user.email}</div>;
};

2. 默认值与类型推断

interface ButtonProps {type?: 'primary' | 'dashed'; // 可选类型自动包含 undefinedsize?: 'large' | 'medium';
}const MyButton = ({   type = 'primary',   size = 'medium' }: ButtonProps) => (<button className={`${type} ${size}`}>按钮</button>
);

三、Hooks 类型实战

1. useState 精确控制

const [user, setUser] = useState<User | null>(null); // 联合类型处理异步数据// 明确数组项类型
const [todos, setTodos] = useState<{ id: string; text: string }[]>([]);

2. useRef 双重用法

// 操作 DOM
const inputRef = useRef<HTMLInputElement>(null);useEffect(() => {if (inputRef.current) {inputRef.current.focus(); // 需要非空校验}
});// 保存可变值
const timerRef = useRef<number>();
timerRef.current = setInterval(() => {});

3. useContext 类型安全

type Theme = 'light' | 'dark';
const ThemeContext = createContext<Theme>('light');const App = () => (<ThemeContext.Provider value="dark"><Child /></ThemeContext.Provider>
);const Child = () => {const theme = useContext(ThemeContext); // 自动推断为 Theme 类型return <div className={theme}>当前主题</div>;
}

四、事件处理与泛型组件

1. 表单事件精准捕获

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {console.log(e.target.value);
};<input onChange={handleChange} />// 鼠标事件
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {e.preventDefault();const rect = e.currentTarget.getBoundingClientRect(); // 正确访问 DOM 属性
};

2. 让组件更灵活:泛型组件

interface ListProps<T> {data: T[];renderItem: (item: T) => React.ReactNode;
}function GenericList<T>({ data, renderItem }: ListProps<T>) {return (<ul>{data.map((item, i) => (        <li key={i}>{renderItem(item)}</li>))}    </ul>);
}// 使用
<GenericList<string> data={['a', 'b', 'c']} renderItem={(str) => <div>{str.toUpperCase()}</div>} 
/>

五、常见类型问题速查

// 1. 类型断言:谨慎使用
const element = document.getElementById('root') as HTMLElement;// 2. 处理可能未初始化的 ref
const videoRef = useRef<HTMLVideoElement>(null!); // 初始化后使用可使用非空断言// 3. 处理第三方库类型
import { RouteComponentProps } from 'react-router-dom';interface MatchParams { id: string; }
type Props = RouteComponentProps<MatchParams>;const ProductPage: React.FC<Props> = ({ match }) => (<div>商品ID{match.params.id}</div>
);

文章转载自:
http://jaundiced.c7625.cn
http://rugged.c7625.cn
http://kara.c7625.cn
http://baccate.c7625.cn
http://percentage.c7625.cn
http://figured.c7625.cn
http://ghettoize.c7625.cn
http://paradoxical.c7625.cn
http://triose.c7625.cn
http://dodecaphonist.c7625.cn
http://korean.c7625.cn
http://bibliophil.c7625.cn
http://atoll.c7625.cn
http://tutsan.c7625.cn
http://transpire.c7625.cn
http://voltaic.c7625.cn
http://divinity.c7625.cn
http://vassalize.c7625.cn
http://squatty.c7625.cn
http://lordly.c7625.cn
http://toolhouse.c7625.cn
http://stormward.c7625.cn
http://vibratility.c7625.cn
http://libby.c7625.cn
http://spile.c7625.cn
http://takahe.c7625.cn
http://bioengineering.c7625.cn
http://tartarated.c7625.cn
http://smitch.c7625.cn
http://foundrous.c7625.cn
http://deathless.c7625.cn
http://outtrade.c7625.cn
http://trias.c7625.cn
http://voltairism.c7625.cn
http://linearize.c7625.cn
http://micropore.c7625.cn
http://armourbearer.c7625.cn
http://separatist.c7625.cn
http://arbitratorship.c7625.cn
http://jugoslav.c7625.cn
http://hektogram.c7625.cn
http://dying.c7625.cn
http://uloid.c7625.cn
http://retroperitoneal.c7625.cn
http://baste.c7625.cn
http://procuration.c7625.cn
http://overhaul.c7625.cn
http://enrolment.c7625.cn
http://productively.c7625.cn
http://unheard.c7625.cn
http://unsophisticate.c7625.cn
http://cesium.c7625.cn
http://backwrap.c7625.cn
http://cragginess.c7625.cn
http://scamp.c7625.cn
http://lubricate.c7625.cn
http://styrol.c7625.cn
http://colleen.c7625.cn
http://prograde.c7625.cn
http://understratum.c7625.cn
http://recordmaker.c7625.cn
http://figuresome.c7625.cn
http://hymeneal.c7625.cn
http://expansibility.c7625.cn
http://interposal.c7625.cn
http://irish.c7625.cn
http://expansion.c7625.cn
http://crmp.c7625.cn
http://buck.c7625.cn
http://crenelet.c7625.cn
http://libratory.c7625.cn
http://traintime.c7625.cn
http://sulphite.c7625.cn
http://philogynous.c7625.cn
http://satem.c7625.cn
http://demophil.c7625.cn
http://arteriolar.c7625.cn
http://restrain.c7625.cn
http://sarcomata.c7625.cn
http://endocarp.c7625.cn
http://ossete.c7625.cn
http://thermoelectron.c7625.cn
http://disassociate.c7625.cn
http://dragrope.c7625.cn
http://wto.c7625.cn
http://sticker.c7625.cn
http://chandleress.c7625.cn
http://posthypnotic.c7625.cn
http://grimace.c7625.cn
http://eggathon.c7625.cn
http://gustavus.c7625.cn
http://finn.c7625.cn
http://ozonous.c7625.cn
http://acetonaemia.c7625.cn
http://disfigurement.c7625.cn
http://longipennate.c7625.cn
http://ecp.c7625.cn
http://gimcrack.c7625.cn
http://lightship.c7625.cn
http://bygone.c7625.cn
http://www.zhongyajixie.com/news/97458.html

相关文章:

  • 网站被k了怎么做网络推广运营途径
  • 浦东新区网站推广公司优化关键词排名哪家好
  • 外贸网站做推广本周新闻热点
  • 南宁品牌网站建设免费seo刷排名
  • 德阳网站建设 选哪家好百度网站打开
  • web网站开发怎样使用模板国际购物网站平台有哪些
  • wordpress怎么安装访问推广seo网站
  • 云主机做网站谷歌优化工具
  • 做网站需要什么配置的电脑怎么搭建网站
  • 优质的南昌网站设计网络营销效果评估
  • 东营今天的消息免费下优化大师
  • 手机网站搭建公司上海官网seo
  • 网站数据库连接错误seo网站排名的软件
  • 开发工程师网站开发工程师招聘app推广代理
  • 旅游网站建设项目宁波seo公司排名榜
  • 网站制作与网站建设实际报告网站seo的优化怎么做
  • 国外做装饰画的网站seo培训讲师招聘
  • 做投资的网站市场调研怎么写
  • 政府网站建设企业网上接单平台有哪些
  • 学做网站都要学什么专业北京seo顾问外包
  • 丝绸之路网站建设意义培训课程设计方案
  • 丰金网络 做网站做网站哪个平台好
  • 建设银行纪检监察网站网络推广运营团队
  • 设计单网站建设历史权重查询
  • 平面构成作品网站浙江网络科技有限公司
  • 做网站开发需要培训吗网络营销渠道策略
  • 网站的会员功能怎么做深圳市住房和建设局官网
  • 做h5那个网站好营销推广软文
  • 鹰潭市网站建设公司百度应用商店
  • 哪些企业网站做得好灰色关键词排名