In react component, location.href doesnt seem to redirect

I have a very simple scenario in which I need to redirect to an external domain in react something like this on change of a switch:

handleSwitch={() => {
                location.href = 'https://www.google.com/'; // oldAppUrl;
                    // window.open(oldAppUrl, '_href')
                }}

However, this is not working. What am I missing?

Answer

Try like this:

window.location.href = 'https://www.google.com/';

and this should work too, using it in an app:

window.location = 'https://www.google.com/';

If none of these two works it means your handleSwitch function is probably not called, then you should show us some more code to help with the solving.

Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .