React Typescript音乐播放器项目笔记:2、歌曲信息页&typescript的用法

19 篇文章 0 订阅
17 篇文章 0 订阅

项目地址https://github.com/BUPTlhuanyu/react-music-lhy

Singer.tsx

import React,{ Component } from 'react'
import './Singer.scss'
import {getSingerList} from 'api/singer.js'
import {ERR_OK} from 'api/config'
import ListView from 'reuse/listview/ListView'
import SingerClass from 'common/js/singer.js'

interface hotType{
    title:string,
    items:Array<any>
}

class mapType {
    hot:hotType;
    [index: string]: hotType;
    constructor(hotName:string){
        this.hot={
            title:hotName,
            items:[]
        }
    }
}

const HOT_SINGER_LEN = 10
const HOT_NAME = '热门'

interface singerProps{

}

interface singerState{
    singers: Array<any>
}

class Singer extends Component<singerProps, singerState>{
    constructor(props: singerProps){
        super(props);
        this.state = {
            singers: []
        }
    }
    componentDidMount(){
        this._getSingerList()
    }
    _getSingerList() {
        getSingerList().then((res) => {
            if (res.code === ERR_OK) {
                this.setState({
                    singers: this._normalizeSinger(res.data.list)
                })
            }
        })
    }
    _normalizeSinger(list:Array<any>) {
        let map = new mapType(HOT_NAME);
        list.forEach((item, index) => {
            if (index < HOT_SINGER_LEN) {
                map.hot.items.push(new SingerClass({
                    name: item.Fsinger_name,
                    id: item.Fsinger_mid
                }))
            }
            const key = item.Findex
            if (!map[key]) {
                map[key] = {
                    title: key,
                    items: []
                }
            }
            map[key].items.push(new SingerClass({
                name: item.Fsinger_name,
                id: item.Fsinger_mid
            }))
        })
        // 为了得到有序列表,我们需要处理 map
        let ret = []
        let hot = []
        for (let key in map) {
            let val = map[key]
            if (val.title.match(/[a-zA-Z]/)) {
                ret.push(val)
            } else if (val.title === HOT_NAME) {
                hot.push(val)
            }
        }
        ret.sort((a, b) => {
            return a.title.charCodeAt(0) - b.title.charCodeAt(0)
        })
        return hot.concat(ret)
    }
    render(){
        const {singers} = this.state
        return(
            <div className="singer">
                <ListView data={singers}/>
            </div>
        )
    }
}

export default Singer

这里需要注意用typescript定义复杂数据结构类型:

interface hotType{
    title:string,
    items:Array<any>
}

class mapType {
    hot:hotType;
    [index: string]: hotType;
    constructor(hotName:string){
        this.hot={
            title:hotName,
            items:[]
        }
    }
}

定义类的时候,可以用 [index: string]: hotType; 的方式来定义类的属性可以自由添加属性名为字符串类型,值为hotType类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值