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.

value not being updated after calling rerender()

See original GitHub issue

Hello everyone! I tried to post on stackoverflow with no luck and neither my search through issues/google gave me any result 😕

So I’m doing a migration. I’ve got this custom hook that I’m testing with @testing-library/react-hooks, jasmine and karma. It’s an AngularJS/React application.

const useSearchParams = () => {
  const $rootScope = useAngularInjection('$rootScope');
  const stateProvider = useAngularInjection('$location');
  const [ url, setUrl ] = useState(stateProvider.search());

  $rootScope.$on('$locationChangeSuccess', () => {
    setUrl(stateProvider.search());
  });

  return url;
};

Where useAngularInjection is another hook that just injects AngularJS stuff (it works fine).

This is my test:

 it('should update the search query from the url when it changes', () => {
      $location.url('https://localhost:8000?all=true&order=title&start=0');
      $rootScope.$apply();

      const { result, rerender } = renderHook(() => useSearchParams());

      $location.url('https://localhost:8000?unscheduled=true&order=title&start=10');
      $rootScope.$apply();

      rerender();

      expect(result.current).toEqual({
        unscheduled: 'true',
        order: 'title',
        start: '10',
      });
 });

At the moment result.current is still {all: 'true', start: '0', order: 'title'} – it’s not updating.

I tried to debug it and it actually goes back into the $locationChangeSuccess branch so I don’t understand why it’s not updating the value of result.current

$rootScope and $location are injected previously and also work fine.

Any idea?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
steoocommented, Jan 7, 2021

I’m on the latest version. Unfortunately, can’t figure out a way of building an easy sandbox for this.

Thank you very much for your help anyway! Never had such responsive support from library maintainers.

0reactions
steoocommented, Jan 12, 2021

Moving to Jest actually solved the issue 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

useState does not update the value on Component Re-render
When using useState we can only initialize it with a value once by passing the value as a param on function call. so...
Read more >
When does React re-render components? - Felix Gerschau
In React hooks, the forceUpdate function isn't available. You can force an update without altering the components state with React.useState like ...
Read more >
How and when to force a React component to re-render
React automatically re-renders components, but what happens when a component is not updating as expected? Find out what you can do here.
Read more >
React doesn't always trigger a re-render on setState
In these cases, React doesn't trigger a re-render because the state did not change. If the current day is 5, it will be...
Read more >
Learn how to force react components to rerender without ...
In class components, you have the option to call force update to force a rerender. In function components, however, there's no chance of ......
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