TypeScript + React

// type script definition
type GreetProps = {
	name:string
	messageCount: number
	isLoggedin:boolean
	name:{
		first:string
		last:string
	}
}
// array props
type PersonList = {
	name:{
		first:string,
		last:string
	}[]
}

// type designated
type StatusProps = {
	status:'loading'| 'success'|'error'
}

	
// react component 
type OscarProps = {
	children: React.ReactNode
}

// optionaol type
type GreetProps = {
	name:string
	messageCount?: number
}
// then in the function, use {messageCount = 0} to set the default value to zero
const {messageCount = 0} = props


// clilck envent
type ButtonProps = {
	handleClick: () => void
}
export const Button = (props: ButtonProps) => {
	return <button onClick={props.handleClick}>click<button/>
}
// then
<Button handleClick={()=> {console.log('click')}} />



// clilck envent with parameter
type ButtonProps = {
	handleClick: (event: React.MouseEvent<HTMLButtonElement>, id: number) => void
}
export const Button = (props: ButtonProps) => {
	return <button onClick={(event)=> props.handleClick(event, 1)}>click<button/>
}
-- then
<Button handleClick={(event, id)=> {console.log('click', event)}} />


// type
type InputProps = {
	value:string
	handleChange:(event: React.ChangeEvent<HTMLInputElement>) => void
}
// could be destructured by ({value, handleChange}: InputProps)
export const Input = (props: InputProps) => {
	return <input type='text' value={props.value} onChange={props.handleChange}>
}


// styles props
type ContainerProps = {
	styles: React.CSSProperties
}

export const Container = (props: ContainerProps) =>{
	return (
		<div styles={props.styles}>
			Hello
		</div>
	)
}

// type 可以export出来

// useState
type AuthUser = {
	name:string
	age:number
}
const [user, setUser] = useState<AuthUser | null>(null);
// because it is optional 
<div> Username is {user?.name}</div>
// 如果不想要null的话,可以
const [user, setUser] = useState<AuthUser>({} as AuthUser)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值