javascript - 如何在 React Native 中使用 sectionList 显示数据?

标签 javascript reactjs react-native

我想在 React Native 中使用列表部分显示数据。我正在使用 reduce() 提取数据,但我无法理解为什么我会收到未定义的行为,说请提供 html 或 uri 作为 react native render html 的 Prop 。

我想将 sectionList 项显示为 HTML 内容,SectionList 的标题是 specificationsData 中的标题。

const specificationsData = specifications.reduce( (acc, curValue) => {

            if (curValue.title !== undefined && curValue.description !== undefined){

                let newObj = {};

                newObj['data'] = curValue.description;

                newObj['title'] = curValue.title;

                console.log(curValue.title)

                acc.push(newObj);

                return acc;
            }

        }, []);

        console.log(specificationsData)

以上日志的结果:

enter image description here

现在我将以上数据传递到SectionList:

import HTMLView from 'react-native-render-html';

<SectionList
                    renderItem={({item, index, section}) => <HTMLView key={index} value={item}/>}
                    renderSectionHeader={({section: {title}}) => (
                        <Text style={{fontWeight: 'bold', color: 'red'}}>{title}</Text>
                    )}
                    sections={specificationsData}
                    keyExtractor={(item, index) => item + index}
                />

如何通过传递 specificationsData 来传递 sectionList 的显示数据和标题?

请看上面代码的截图:

enter image description here

最佳答案

根据SectionList documentation ,数据需要是您的 specificationsData 中的一个数组。更新以下行:

newObj['data'] = [curValue.description];

关于javascript - 如何在 React Native 中使用 sectionList 显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52908407/

相关文章:

react-native - 错误 : React Refresh runtime should not be included in the production builds

javascript - Jquery Treeview 切换问题

javascript - 谷歌地图 - FusionTablesLayer 到多边形

javascript - 不能在 React 中将 react-input-mask 与子组件一起使用

javascript - 错误 : attempted to update component that has already been unmounted (or failed to mount)

reactjs - 如何在 React Native 中使用 i18next 显示文本和超链接

javascript - React Native Firebase 事务无法正常工作

javascript - 尝试获取 span 元素的内部文本内容

javascript - 如何对事件对象进行字符串化?

reactjs - React 如何返回另一个组件的 div 和变量值