Issue
I'm using a webview in react native and I want to display a custom error message when some error occurs (e.g. no internet connection).
My code:
<WebView
renderError={() => (
<MissingConnection />
)}
other params....
/>
When there is an error loading the webpage, for a fraction of a second the webview displays the default android error, such as:
then my
MissingConnection
component pops up, hiding the webview.
Is there a way to completely remove the default android error screen? It flashes for just a fraction of a second and the result feels really wrong.
Solution
First of all: You are doing it right.
I observed the same problem recently and did some investigation. The problem lies not in your code or React Native or react-native-webview
.
This is just the default behavior for Android's WebView
. Many Java developers encounter the same problem, examples for related threads on SO:
webview error while loading a page without internet
Prevent WebView from displaying "web page not available"
Android WebView onReceivedError()
The usual proposals to work around are:
check the internet connection before trying to load anything (prevent failing)
remove the error content quickly and show your own content in the
onReceivedError
( which basically maps to yourrenderError
method inreact-native-webview
). Sometimes with loading a local url like done in Java here.take care of having an overlay which is removed in case there is no error at all.
react-native-webview
does it the other way round, showing an overlay when there is an error. But the Activity indicator overlay is a good example, it says until loading has finished or encountered an error.
As far as I know there is nothing we can do about except these disappointing ways, as I would prefer not to fight against the system.
Edit: Firefox Focus for Android does the same with quickly replacing content in the error handler.
Thats done in Java in their source here:
and
So I assume we are in good company!
Edit 2: I am curious if this is really visible when not in debug mode on a real Android device. My educated guess is that the code executes way faster and it shouldn`t be visible at all. Btw this page is probably only shown for 404 (Not found) errors which are unlikely if you use hardcoded urls and your own servers.
Edit 3: The native error page is visible running on a real device in release mode. The only way to prevent this flickering would be to create an overlay. I opened an issue related to another error which also addresses this one with react-native-webview
here https://github.com/react-native-community/react-native-webview/issues/474#issuecomment-487022106.
Answered By - Frederik Winkelsdorf
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.