react-native - 为什么我调用一次时会打印无限值?
问题描述
它不断调用 addName 函数无限 -
- 世博会挂钩 SQLite 。
//hook for displaying the names in the database
const [names, setNames] = useState([]);
console.log(names);
//inserts a name into the database, calls update if successful
const addName = () => {
let newName = `${"empty"}-${"empty"}`;
db.transaction((tx) => {
tx.executeSql("INSERT INTO Names(name) VALUES (?)", [newName], update());
});
//add the results of the database into the names hook
const update = () => {
console.log(db);
db.transaction((tx) => {
tx.executeSql(
"SELECT name from Names",
[],
(tx, { rows }) => {
setNames(() => {
let retRA = [];
rows._array.forEach((elem) => {
retRA.unshift(elem.name);
});
return retRA;
});
},
(tx, error) => {
console.log("ERRORE-->SQLITE--->", error);
}
);
});
};
};
addName();
解决方案
看起来您正在调用addName
组件的主体。因为addName
更新状态,这将导致无限循环。
addName
仅在需要时(用户按下按钮后)或组件安装时尝试调用。
推荐阅读
- android - 在 React Native 的深色主题中,Android 白色视图出现在键盘前
- javascript - 修改 SVG 地图上标记的标记
- visual-studio - 如何在计算机上测试 Unity Touch Controls?
- java - 有没有办法以编程方式在android中同时运行wifi和以太网?
- openlayers - 非 Web 墨卡托投影中的 XYZ
- javascript - 如何将 var 值从一个函数移动到另一个函数?Javascript
- arrays - 从 jsonb 列的 json 属性中的数组中获取数组元素
- entity-framework - 我无法在 .NET Core 3.1.9 中使用实体框架创建新的 Web API
- r - 正态混合分布
- python - 在 Python 中以递归方式返回由列表表示的树的节点