【问题标题】:Why prints infinite value when I call it once?为什么我调用一次时会打印无限值?
【发布时间】:2021-01-12 05:27:32
【问题描述】:

它不断地调用 addName 函数 -

  • expo 挂钩 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();

【问题讨论】:

    标签: react-native sqlite expo hook


    【解决方案1】:

    看起来您正在组件的主体中调用addName。因为addName 会更新状态,这将导致无限循环。

    仅在需要时(用户按下按钮后)或组件安装时尝试调用addName

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      相关资源
      最近更新 更多