我正在尝试用 SQLite 数据库中的数据填充 SectionList。
我从这里开始:
constructor(props) {
super(props);
this.state = {
loading: true,
sectionListData: [
{
title: 'A Bulls',
data: []
},
{
title: 'H Bulls',
data: []
},
{
title: 'C Bulls',
data: []
},
{
title: 'R Bulls',
data: []
}
]
};
}
我从数据库中获取数据,当我将 setState 转到适当的位置时,它不会占用。
componentDidMount() {
this.aBulls();
this.cBulls();
this.hBulls();
this.rBulls();
}
每个函数都是相同的,从各自的数据库中获取数据:
aBulls() {
db.transaction(
tx => {
//SQL Statement
tx.executeSql("select * from abulls group by reg",
//Arguments
[],
//Success
(tx, { rows: {_array} }) => {
const handlebull = JSON.stringify(_array);
const bulls = JSON.parse(handlebull);
this.setState({sectionListData: [
{
0:
{
data: bulls
}
}
]
});
this.setState({loading: false});
},
//Error
(error) => {console.log(error)}
);
}
)};
console.log(bulls) 将按预期返回一个数据数组。console.log(this.state.sectionListData[0].data) 将返回“未定义”。我看不到它来更新 SectionList 的嵌套数组的索引。