Reputation: 61
My fetch function is sometimes returning errors. If that happens I would like to go to something like url/notfound and render NotFound component. Can it be done with react router? Are there any other ways to change route from function ?
Upvotes: 0
Views: 262
Reputation: 827
You can show something like this
I am showing ES6 example
Let's assume that you have a state variable which updates loadError
on fetch
operations result
this.state={
loadError:false
}
Like,
fetch(this.host+this.url,{method:"GET",redirect:"follow"}).then(()=>
this.setState({loadError:true})
)
somewhere in render()
method
{loadError ? <Redirect to="/NotFound"/>}
Upvotes: 1