react+TypeScript基础配置

本文介绍了如何进行React与TypeScript的基础配置,包括详细的文件结构、每个关键文件如store/index.ts、store/action/channel.ts、reducers/channel.ts以及App.js的功能解析,并通过类型调用图解帮助理解其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.基础文件结构

 

 二.文件详情

1.store/index.ts

import { applyMiddleware, createStore } from 'redux'
import rootRouter from './reducers'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunk, { ThunkAction } from 'redux-thunk'
import { ChannelAction } from './action/channel'
import { ArtilceAction } from './action/article'
const store = createStore(
  rootRouter,
  composeWithDevTools(applyMiddleware(thunk))
)

// 一劳永逸解决 useSelector 类型问题
console.log(11111, store.getState())
type Fn = typeof store.getState
export type RootState = ReturnType<Fn>

// 解决thunk调用函数时类型问题
export type RootAction = ThunkAction<
  void,
  RootState,
  unknown,
  ChannelAction | ArtilceAction
>
export default store

2.store/action/channel.ts

import axios from 'axios'
import { RootAction } from '..'
import { Channel } from '../reducers/channel'
// 导出给store/index.ts  解决dispatch返回函数类型问题
export type ChannelAction =
  | {
      type: 'INIT_CHANNEL'
      data: Channel[]
    }
  | {
      type: 'SET_CUR_CHANNEL'
      id: number
    }
// 在这个函数后面使用导入进来的RootAction解决返回函数类型的问题
export function getChannel (): RootAction {
  return async (dispatch) => {
    const res = await axios.get('http://geek.itheima.net/v1_0/channels')
    console.log(res)
    dispatch({
      type: 'INIT_CHANNEL',
      data: res.data.data.channels
    })
  }
}

3.reducers/channel.ts

// 解决action类型问题
import { ChannelAction } from '../action/channel'
// 解决data(channelList)类型问题,在store/action/channel.ts中也会用到
export type Channel = { id: number; name: string }
// 解决初始数据类型问题
type ChannelState = {
  curChannelId: number
  channelList: Channel[]
}

const initState: ChannelState = {
  curChannelId: 0, // 当前选中频道id
  channelList: [] // 所有频道信息
}

export default function channel (state = initState, action: ChannelAction) {
  console.log(action, 'channel')
  if (action.type === 'INIT_CHANNEL') {
    return { ...state, channelList: action.data }
  } else if (action.type === 'SET_CUR_CHANNEL') {
    return { ...state, curChannelId: action.id }
  }
  return state
}

4.App.js

import React from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { RootState } from '../store'

function Channel () {
    const channelList = useSelector(
    // 解决state类型问题
    (state: RootState) => state.channel.channelList
    )
    return(xxx)
}

export default Channel

三.类型调用图解

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值