value not being updated after calling rerender()
See original GitHub issueHello 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:
- Created 3 years ago
- Comments:11 (7 by maintainers)
Top 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 >
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
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.
Moving to Jest actually solved the issue 😃