【发布时间】:2020-01-23 02:56:58
【问题描述】:
我正在制作一个列表视图,当数据源列表更新时,它必须每隔几秒动态更改一次。
从中获取数据的列表本质上是
const area = [a1,a2,a3,.....,a50]
从这个列表中,我 setState([]) 的另一个列表 - region 使用 array.map 渲染到 Frame 中
我尝试了https://www.robinwieruch.de/react-function-component的各种方法
我正在使用useEffect()对应该呈现的数组regions进行更改
export function DynamicList() {
const [region, setRegion] = React.useState([])
useEffect(() => {
setInterval(() => {
//take a random integer between 0 and length of the main array - areas
// if it already exists in the smaller array - regions, then delete it at that index
if (region.indexOf(areas[randNum]) != -1) {
// find the index to delete
delIndex = region.indexOf(areas[randNum])
console.log(areas[randNum] + " " + delIndex)
//delete the item at that index
region.splice(delIndex, 1)
}
// adding the random itemt o first position
region.splice(0, 0, areas[randNum])
region.join()
setRegion(region)
console.log(region)
}, 6000)
}, [])
}
Console.log 正在运行,我看到 Region 数组正在更新,但它没有在预览部分呈现
渲染UI的部分是
return(
<Stack>
{region.map((item, index) => {
return (
<Frame
background=""
key={index}
defaultValue={item}
height={10}
width={200}
radius="4%"
color="ffffff"
style={{
fontFamily: "Lato",
fontSize: 10,
}}
>
{item}
</Frame>
)
})}
</Stack>
)
我希望在预览中,随着每个新城市添加到显示在现有列表上方的 region 数组中,城市会不断更新。
编辑:已解决。下面回答
【问题讨论】:
-
您在哪里以及如何更新
region数组? -
Harish,我使用
SetInterval在useEffect中每6 秒更新一次region数组。我正在更新区域数组,然后将其传递给setRegion() -
你的渲染逻辑和
DynamicList是否在同一个功能组件中?从问题来看,似乎是分开的。或者你能发布你的完整渲染组件吗?
标签: javascript reactjs framerjs