首页 > 其他分享 >React与TS的配合

React与TS的配合

时间:2023-01-18 00:44:53浏览次数:50  
标签:react const TS 配合 React return export import

React与TS配合

props限制

npx create-react-app react-ts-study --template typescript

src/index.tsx

import App from './01_react-ts-props';

src/01_react-ts-props.tsx

import React from 'react'

interface WelcomeProps {
  msg?: string //可选
  count: number
  list: string[]
  info: { username: string; age: number }
  state?: 'loading' | 'success' | 'error'
}

const Welcome = (props: WelcomeProps) => {
  //设置默认值
  const { count = 0 } = props
  return <div>Welcome,{count}</div>
}

// const Welcome: React.FC<WelcomeProps> = (props) => {
//设置默认值
//   const { count = 0 } = props
//   return <div>Welcome,{count}</div>
// }

export default function App() {
  return (
    <div>
      <Welcome
        msg="hello"
        count={123}
        info={{ username: 'alex', age: 20 }}
        list={['1', '2']}
        state={'loading'}></Welcome>
    </div>
  )
}

children与event的限制

import React from 'react'

interface WelcomeProps {
  children?: React.ReactNode
  handleMsg?: (ev: React.MouseEvent<HTMLButtonElement>) => void
}

const Welcome = (props: WelcomeProps) => {
  const handChange = (ev: React.ChangeEvent<HTMLInputElement>) => {}
  return (
    <div>
      Welcome
      <button onClick={props.handleMsg}>点击</button>
      <input type="text" onChange={handChange} />
    </div>
  )
}

export default function App() {
  return (
    <div>
      <Welcome handleMsg={(ev) => {}}>
        <div>div</div>
        123
      </Welcome>
    </div>
  )
}

style与component

import React from 'react'

interface WelcomeProps {
  style: React.CSSProperties
  component: React.ComponentType<HeaderPros>
}

interface HeaderPros {
  username: string
}

const Welcome = (props: WelcomeProps) => {
  return (
    <div>
      Welcome
      <props.component username="alex"></props.component>
    </div>
  )
}

function Header(props: HeaderPros) {
  return <div>hello Header</div>
}

export default function App() {
  return (
    <div>
      <Welcome component={Header} style={{ border: '1px red solid' }}></Welcome>
    </div>
  )
}

use函数限制

import React, { useEffect, useRef, useState } from 'react'

interface WelcomeProps {}

const Welcome = (props: WelcomeProps) => {
  return <div>Welcome</div>
}

export default function App() {
  // const [count, setCount] = useState(0)
  const [count, setCount] = useState<string | number>(0)
  const [list, setList] = useState<string[]>([])
  const [info, setInfo] = useState<{ username: string; age: number } | null>(
    null
  )
  const myRef = useRef<HTMLButtonElement>(null)

  useEffect(() => {
    console.log(myRef.current?.innerHTML)
    return () => {}
  }, [])
  const handleClick = () => {
    setCount('1')
    setList(['123'])
    setInfo({ username: 'alex', age: 23 })
  }
  return (
    <div>
      <button onClick={handleClick}></button>
      <Welcome></Welcome>
      {count},{list}
    </div>
  )
}

对路由的限制

npm i react-router-dom

src/index.tsx

import React from 'react'
import ReactDOM from 'react-dom/client'
import './index.css'
// import App from './App'
import reportWebVitals from './reportWebVitals'
import { RouterProvider } from 'react-router-dom'
import router from './router'

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
root.render(
  <React.StrictMode>
    <RouterProvider router={router}></RouterProvider>
  </React.StrictMode>
)
reportWebVitals()

src/router/index.ts

import { createBrowserRouter } from 'react-router-dom'
import Index from '../views/Index/Index'
import Login from '../views/Login/Login'
import User from '../views/User/User'
import App from '../App'
import React from 'react'
import type { RouteObject } from 'react-router-dom'

declare module 'react-router' {
  interface NonIndexRouteObject {
    meta?: { title: string }
  }
  interface IndexRouteObject {
    meta?: { title: string }
  }
}
export const routes: RouteObject[] = [
  {
    path: '/',
    element: React.createElement(App),
    meta: { title: '/' },
    children: [
      {
        path: 'index',
        element: React.createElement(Index),
        meta: { title: 'index' },
      },
      {
        path: 'user',
        element: React.createElement(User),
      },
      {
        path: 'login',
        element: React.createElement(Login),
      },
    ],
  },
]

const router = createBrowserRouter(routes)

export default router

src/App.tsx

import { Outlet } from 'react-router-dom'
function App() {
  return (
    <div className="App">
      <Outlet></Outlet>
    </div>
  )
}

export default App

RTK

npm i @reduxjs/toolkit
npm i react-redux

src/index.tsx

import React from 'react'
import ReactDOM from 'react-dom/client'
import './index.css'
// import App from './App'
import reportWebVitals from './reportWebVitals'
import { RouterProvider } from 'react-router-dom'
import router from './router'

import { Provider } from 'react-redux'
import store from './store'

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
root.render(
  <React.StrictMode>
    <Provider store={store}>
      <RouterProvider router={router}></RouterProvider>
    </Provider>
  </React.StrictMode>
)

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals()

src/store/modules/user.ts

import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'

export const loginAction = createAsyncThunk(
  'users/loginAction',
  async (userId: number) => {
    const response = await new Promise((resolve) => {
      resolve('response data')
    })
    return response
  }
)

const userSlice = createSlice({
  name: 'user',
  initialState: {
    name: 'alex',
  },
  reducers: {
    //限定payload类型
    change(state, action: PayloadAction<string>) {
      state.name = action.payload
    },
  },
})

//限定payload类型
export const { change } = userSlice.actions

export default userSlice.reducer

src/store/index.ts

import { useDispatch } from 'react-redux'
import { configureStore } from '@reduxjs/toolkit'

import userReducer from './modules/user'

const store = configureStore({
  reducer: {
    user: userReducer,
  },
})

export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch
export const useAppDispatch: () => AppDispatch = useDispatch

export default store

src/views/Index/Index.tsx

import React, { useState } from 'react'
import { useSelector } from 'react-redux'
import { RootState, useAppDispatch } from '../../store'
import { change, loginAction } from '../../store/modules/user'
import { Button, Rate } from 'antd'

export default function Index() {
  const name = useSelector((state: RootState) => state.user.name)
  const dispatch = useAppDispatch()
  const handleClick = () => {
    // dispatch({
    //   type: 'user/change',
    //   payload:'yuan'
    // })//这种写法没法限制payload类型
    dispatch(change('yuan')) //限定payload类型
    dispatch(loginAction(123))
  }
  const [rate, setRate] = useState(3)
  return (
    <div>
      <Button type="primary" onClick={handleClick}>
        点击
      </Button>
      Index,{name}
      <Rate value={rate} onChange={setRate}></Rate>
    </div>
  )
}

antd

npm i antd

src/views/Index/Index.tsx

import React, { useState } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import type { RootState } from '../../store'
import { change } from '../../store/modules/user'
import { Button, Rate } from 'antd'

export default function Index() {
  const name = useSelector((state: RootState) => state.user.name)
  const dispatch = useDispatch()
  const handleClick = () => {
    // dispatch({
    //   type: 'user/change',
    //   payload:'yuan'
    // })//这种写法没法限制payload类型
    dispatch(change('yuan')) //限定payload类型
  }
  const [rate, setRate] = useState(3)
  return (
    <div>
      <Button type="primary" onClick={handleClick}>
        点击
      </Button>
      Index,{name}
      <Rate value={rate} onChange={setRate}></Rate>
    </div>
  )
}
image-20230118000332216

标签:react,const,TS,配合,React,return,export,import
From: https://www.cnblogs.com/hwq1009/p/17058998.html

相关文章

  • React路由与RTK的使用
    ReactRouter与ReduxReactRouterV6.4npmireact-router-domnpminstallsasssrc/App.js//importlogofrom'./logo.svg';import'./App.css';import{Link,Ou......
  • 安装react脚手架,运行
    npmuninstallcreate-react-app-g(以前创建过脚手架的话先进行卸载再重新安装以适配最新版本此步骤可逃过失败也是正常现象)npminstallcreate-react-app-g安装脚手......
  • React:开发者工具谷歌插件下载安装
    React:开发者工具谷歌插件最近学习前端react主要是想大概浏览一下,这里提供尚硅谷的是视屏资料中的谷歌插件的下载。(如有侵权联系删除)这里提供我的下载地址,其中除了插件,还有......
  • ECharts 仪表盘 实现进度条渐变色,但数据轴渐变颜色范围固定
    需求实现渐变色的仪表盘,进度条为渐变色,但整个数据轴的颜色范围固定,如下两图:难点该需求难点在于,正常思路配置仪表盘时,为数据设置渐变色,渐变范围是从起点到数据值(如下图......
  • 自定义react项目脚手架
    呀呀呀呀,快过年啦,开心!而且之前一直想做的2个事,一个从0到1自定义开发ui组件库(从0到1开发一个自己的ui库)已经做好啦,还有一个做个自定义的脚手架,现在也做好啦。2个flag完成,开......
  • 揭开华为云CodeArts TestPlan启发式测试设计神秘面纱!
    摘要:质量是产品的生死线。本文分享自华为云社区《​​揭开华为云CodeArtsTestPlan启发式测试设计神秘面纱!​​》,作者:华为云PaaS服务小智。2019年12月20日,是美国波音公司新......
  • 揭开华为云CodeArts TestPlan启发式测试设计神秘面纱!
    摘要:质量是产品的生死线。本文分享自华为云社区《揭开华为云CodeArtsTestPlan启发式测试设计神秘面纱!》,作者:华为云PaaS服务小智。2019年12月20日,是美国波音公司新一代......
  • React: 路由重定向
    解决方案参考链接https://v5.reactrouter.com/web/example/route-config......
  • React:styled-components有趣的用法
    背景用于记录一些styled-components的有趣的用法绑定a标签的链接编写伪类在styleComponents中使用参数传入参数......
  • pycharm报错提示:无法加载文件\venv\Scripts\activate.ps1,因为在此系统上禁止运行脚
    pycharm报错提示:无法加载文件\venv\Scripts\activate.ps1,因为在此系统上禁止运行脚本。解决办法1.终端输入get-executionpolicy,回车返回Restricted。 2.复制Windo......