UseEffect: Failing trying to use another hook
See original GitHub issueCode Sandbox: https://codesandbox.io/s/react-router-forked-mnnxb
In the above sandbox I wanted to use a useEffect
hook inside my shared hook. For example I want to write a shared hook that listens for location
changes and updated the document title like this from React Router…
import React, { useState, useEffect } from "react";
import { useBetween } from "use-between";
import { useLocation } from "react-router-dom";
const useShared = () => {
const location = useLocation();
const [value, setValue] = useState(false);
useEffect(() => {
document.title = `Location: ${location}`;
setValue(true);
}, [location]); // Only re-run the effect page location changes
return {
value,
setValue
};
};
export const useSharedHook = () => useBetween(useShared);
Is my use case wrong because I get this error:
TypeError
Invalid attempt to destructure non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
React.useEffect Hook – Common Problems and How to Fix ...
The first and probably most obvious option is to remove the dependency from the useEffect dependency array, ignore the ESLint rule, and move...
Read more >Why is my React useEffect() hook throwing an Uncaught ...
I've tried using no dependencies in the useEffect() statement (which caused an infinite loop), I've tried using all the suggested dependencies ...
Read more >The last guide to the useEffect Hook you'll ever need
Understanding how the useEffect Hook works, and why it requires a wholesale shift in mindset, is essential to writing modern React code.
Read more >Dangers of using Objects in useState & useEffect ReactJS ...
Another solution would be to simply not use objects in an useState hook. You could use the useState hook per each property of...
Read more >Rules of Hooks - React
Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Nice!!!
also for the error “Invalid attempt to destructure non-iterable instance.” is there any way to catch that error and give the user a better error message about their invalid use of use-between? Because I was stumped at first on what the error was trying to tell me. 😃