This piece of code will run infinitely, but no matter removing the useValue(object)
or useTest()
will stop the infinite run. This whole thing does not make sense to me, can somebody explain it?
https://codesandbox.io/s/zen-currying-v2zu1?file=/src/App.js
import React from "react";
import { useEffect, useState } from "react";
import "./styles.css";
export default function App() {
// this code can also stop the infinite loop
// const [object] = useState({
// name: "",
// personId: ""
// });
const object = {
name: "",
personId: ""
};
// strange thing: remove useValue or useTest can stop the ifinite loop
useValue(object);
useTest();
// this console will run infinitely
console.log(object);
return <div className="App">{"123"}</div>;
}
export const useTest = () => {
const [a, setA] = useState(1);
useEffect(() => {
setA(2);
}, []);
};
const useValue = (value) => {
const [a, setA] = useState(value);
useEffect(() => {
setA(value);
}, [value]);
// return a
};