question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UseEffect: Failing trying to use another hook

See original GitHub issue

Code 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:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mellowarecommented, Feb 7, 2022

Nice!!!

1reaction
mellowarecommented, Dec 6, 2021

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. 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found