react学习笔记(八)---react与ts搭配时的类型声明小结


1、在ts中函数式组件的声明

import {PropsWithChildren} from "react";

interface IItemProps {
    name: string,
    age: number

}
const Item: React.FC<IItemProps>= (props: PropsWithChildren<IItemProps>) => {
    return <div>
        <h2>this is Item</h2>
        <div><span>name---{props.name}</span><span>age---{props.age}</span></div>
    </div>
}

export default Item

 2、高阶组件的类型声明

import {PropsWithChildren} from "react";

interface IHighItem{
    name: string,
    age: number
}
const withHighItem: (Comp: React.ComponentType<any>) => React.FC<IHighItem> = (Comp: React.ComponentType<any>) => {
    return (props: PropsWithChildren<IHighItem>) => {
        return <div>
            <h3>this is highItem</h3>
            <Comp {...props}/>
        </div>
    }
}

export default withHighItem

使用

import React from "react";
import style from './App.module.css'
import Item from './Item'
import HighItem from './HighItem'

const App: React.FC = () => {
  return <div className={style.App}>
    <h1>this is App Test</h1>
    {HighItem(Item)({name: 'abc', age: 12})} </div>
}

export default App;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM