Skip to content

Instantly share code, notes, and snippets.

@GitaiQAQ
Last active January 20, 2021 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitaiQAQ/e5b768898325031dcd079eea22c4916a to your computer and use it in GitHub Desktop.
Save GitaiQAQ/e5b768898325031dcd079eea22c4916a to your computer and use it in GitHub Desktop.
import React, { Component, useCallback, useRef, useState } from 'react'
import { View, Text, Button } from '@tarojs/components'
import VirtualList from '@tarojs/components/virtual-list'

function buildData(offset = 0) {
  return Array(100)
    .fill(0)
    .map((_, i) => i + offset)
}

const Row = React.memo(({ id, index, style, data }) => {
  return (
    <View
      id={id}
      className={index % 2 ? 'ListItemOdd' : 'ListItemEven'}
      style={style}
    >
      Row {index} : {data[index]}
    </View>
  )
})

export default function App() {
  const [data, setData] = useState(buildData(0))
  const dataLen = data.length
  const ref = useRef(null)
  const onClick = useCallback(() => {
    // @ts-ignore
    ref.current.scrollTo(200)
  }, [])
  return (
    <View>
      <VirtualList
        ref={ref}
        height={500} /* 列表的高度 */
        width='100%' /* 列表的宽度 */
        itemData={data} /* 渲染列表�的数据 */
        itemCount={dataLen} /*  渲染列表的长度 */
        itemSize={20} /* 列表单项的高度  */
      >
        {Row}
      </VirtualList>
      <Button onClick={onClick}>test</Button>
    </View>
  )
}

微信小代码

https://developers.weixin.qq.com/s/tScDCvmv73nh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment