Ken428965 2022-10-20 13:35 采纳率: 53.6%
浏览 50
已结题

react,如何将新的数据加入到antd mobile的IndexBar序列的顶部?

在使用的react版本如下:

img


已写的代码:

import React from 'react';
import { IndexBar, List } from 'antd-mobile';

const charCodeOfA = 'A'.charCodeAt(0);
const groups = Array(26)
    .fill('')
    .map((_, i) => ({
    title: String.fromCharCode(charCodeOfA + i),
    items: [],
}));
export default () => {
    return (<div style={{ height: window.innerHeight - 45 }}>
      <IndexBar>
        {groups.map(group => {
            const { title, items } = group;
            return (<IndexBar.Panel index={title} title={`${title}`} key={`${title}`}>
              <List>
                {items.map((item, index) => (<List.Item key={index}>{item}</List.Item>))}
              </List>
            </IndexBar.Panel>);
        })}
      </IndexBar>
    </div>);
};


目前效果:

img


希望实现以下效果——自动添加#和hot到右边列顶部,且在左边内容区域显示当前城市和热门城市两项标题:

img


请问代码应该如何修改?

  • 写回答

2条回答 默认 最新

  • 崽崽的谷雨 2022-10-20 13:38
    关注

    这个 不就是 groups 生成的 。你要加 热 那就 给 groups 里加一项,加到最前面 。然后 items 加入数据就行。
    [{title:"热",itemes:[xxx]}]

    let s = {
                    title: "热", items: [{
                        // "label": "天津",
                        // "value": "AREA|re",
                        // "pinyin": "tianjin",
                        // "short": "tj"
                    }]
                };
                setGroups([s,...v]);
    
    

    img

    对了 useEffetc 这里 你需要
    useEffect(() => {},[]) 这样写 ,之前给你的例子有的小问题 。你把 回调里的groups 去掉 。要不然 会重复执行很多次

    img

    噢 如果 有key值的警告就把 s加个key值

    img

    img


    然后 key 这么写 key || title 。因为不加会有警告

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月21日
  • 已采纳回答 10月20日
  • 创建了问题 10月20日

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀