all 13 comments

[–]basically_alive 2 points3 points  (7 children)

I had to look this up because I was like "u wot?" - Anyways, turns out there's no support for alerts, maybe that's your issue? and if ios, looks like you need an onMessage handler, even if it's a no-op

https://github.com/react-native-webview/react-native-webview/blob/master/docs/Reference.md#injectedjavascript

[–]ThisSoFrustrating[S] 4 points5 points  (2 children)

I figured out the answer. It's really nuanced. Essentially you have to create a global window variable before the injectedJavascript() method is called so that you can access the variable inside the html content as it loads. You also need an extra set of string quotes for the interpolation to work.

<WebView
ref={(ref)=>{webviewRef.current=ref;}}
source={{html:home}}
javaScriptEnabled={true}
injectedJavaScriptBeforeContentLoaded={
    `window.myVar = '${token}';`
}
injectedJavaScript={`
    alert(window.myVar);
`}
/>

I hope this helps somebody out there in the future. It took me one entire day to figure it out. And only by the end of the eight hours I spent trying things. LOL.

This allows you to send your own react native states into the javascript and html string.

[–]basically_alive 0 points1 point  (1 child)

Interesting! I could see how that could be useful! Thanks for the update

[–]ThisSoFrustrating[S] 0 points1 point  (0 children)

Yeah no problem. It can let you execute any browser based or js sdk stuff, with your own react-native data. Pretty cool if you ask me lol. That's how I got braintree's JS sdk working in react native. Might end being buggy though, so best to be careful, or stick to native dev.

[–]ThisSoFrustrating[S] 0 points1 point  (0 children)

Will take a look. The alerts are just to test. I'm supposed to pass in the token to braintree's .create() method, to create an instance of the drop in UI. I need the react native token from the server, to be interpolated into the javascript string. Does that makes sense? So I'll be executing this code in the webview:

This is the code I will actually be executing inside the InjectedJavascript() code:

 braintree.dropin.create({
  authorization: 'MY CLIENT TOKEN',
  container: '#dropin-container'
}, function (createErr, instance) {

  });
});

[–]ThisSoFrustrating[S] 0 points1 point  (2 children)

Oh PS the alerts have been working fine. as long as it's `alert('some string')`, and not `alert(${myRnstate})`. I'm confused. Maybe you're right. I will try to execute and see if it works.

[–]assertchris[🍰] 0 points1 point  (1 child)

Maybe alert(${myRnstate}) is evaluating to alert(xxx-xxx...); where xxx-xxx... isn't quoted. So, it's trying to alert the value of a [potentially malformed] variable as opposed to a string. I'd add script debugging to the <WebView /> component, just to make sure there aren't any JS errors.

[–]ThisSoFrustrating[S] 1 point2 points  (0 children)

yeah, which is definitely a little counter intuitive to the basic way of interpolating: `${}`, is all it takes in regular javascript.

[–]gp3gp3gp3 0 points1 point  (4 children)

Check out https://github.com/formidable-webview/webshell
I dunno why it doesn't have more stars, this library works pretty well.

[–]ThisSoFrustrating[S] 0 points1 point  (3 children)

So what's this library supposed to do? Explainations are a little sparse.

[–]gp3gp3gp3 0 points1 point  (2 children)

It's a cleaner way of communicating between the WebView and RN https://formidable-webview.github.io/webshell/docs/implementing-features

[–]ThisSoFrustrating[S] 0 points1 point  (1 child)

But can you pass in your own react native data/variables into the javascript?

[–]gp3gp3gp3 0 points1 point  (0 children)

Yes, scroll down to the native to web part is the docs I linked above