P粉1569834462023-09-02 00:17:20
This intermediate function seems to be the problem:
function usePage() { return React.useState(1); }
The state setting function returned by
useState itself is not recreated on each render, but calling it via this external function seems to break that. This causes the dependencies to change, as observed in the useEffect
dependencies array.
If you don't need this usePage
function (in the example you don't, but there may be cases where it makes sense in a larger context), then just remove it entirely and use it directly useState
:
const [page, setPage] = useState("");
Generally, one doesn't want to call hooks inside a function anyway, as it can quickly and easily lead to violating the rule that hooks must be called consistently and in the same order on every render. Therefore, it's best to keep hook calls in the body of the component itself (and early in the component's logic).
Sometimes, there are situations where you really want a custom function to act as a dependency in useEffect
. Since the declared function is redeclared on every render, you have the problem you encountered above. To solve this problem, you can wrap the function in a useCallback
hook, which itself also has a dependency array, very similar to useEffect
. The purpose of this hook is to create custom functions that React can cache as a single function instance across renders (unless dependencies change). For example:
const myCustomFunction = useCallback(args => { // use function args, etc. }, [someDependencies]);