简体   繁体   中英

Absolute path redirect in React

I'm trying to build a bit.ly clone and I would like to redirect to an exact URL.

Unfortunately if I use window.location.replace(str) in some cases it believes I would like to redirect to a relative URL. For example str = "google.com".

I'm wondering if there is a way to force an absolute path redirect that I am missing

Use a URL with a schema. google.com is a relative URL, like image.jpg .

 location.replace('google.com'); 

 location.replace('http://stackoverflow.com'); 

import {withRouter} from 'react-router-dom';

Try importing withRouter and wrap the component with it.

export default (withRouter(MyComponent))

Now, you can use

this.props.history.replace('// your link');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM