0

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 ?

1 Answer 1

1

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"/>}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.