1

When user clicks on <a>, I want to first get a new href resolved by a promise, and then trigger the change of window location. But I can't find a good practice to do this.

To conclude, here's psuedo code:

getNewUrl = (e) => {
  e.preventDefault();
  fetchUrl.then(newUrl => {
     newUrl = newUrl;
     e.resumeEvent();
  })
}

<a 
  href={newUrl}
  onClick={this.getNewUrl}
/>

But obviously there's no e.resumeEvent(). What should I do to handle this? (Don't want to fetch url massively in componentDidMount(), as there might be many requests)

Stanley Luo
  • 3,689
  • 5
  • 34
  • 64
  • I have found a solution here: http://stackoverflow.com/questions/35206589/how-to-download-fetch-response-in-react-as-file It's workable but I'm surprised that it needs so much effort for such a common task. Any better solution is welcome. – Stanley Luo Oct 12 '16 at 05:14

1 Answers1

0

It sounds like you just want to change a window location.

window.location = newUrl

should do that trick.

theptrk
  • 730
  • 9
  • 17
  • Hmm two issues with this: 1. Expecting to see a new tab being opened, not changing the current window location, as the url is for downloading; 2. Since it's a React context, kinda prefer to solve in a React way. – Stanley Luo Oct 12 '16 at 02:26