-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a postMessage function and an onMessage event for webviews … #9762
Changes from all commits
426b2f8
dec7c42
2aded21
efce5f1
62edd77
d0f0341
dfd4db2
a508f46
4e4b27c
762b708
3df06ba
3c81714
ff94739
1d67a78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,6 +217,53 @@ class ScaledWebView extends React.Component { | |
} | ||
} | ||
|
||
class MessagingTest extends React.Component { | ||
webview = null | ||
|
||
state = { | ||
messagesReceivedFromWebView: 0, | ||
message: '', | ||
} | ||
|
||
onMessage = e => this.setState({ | ||
messagesReceivedFromWebView: this.state.messagesReceivedFromWebView + 1, | ||
message: e.nativeEvent.data, | ||
}) | ||
|
||
postMessage = () => { | ||
if (this.webview) { | ||
this.webview.postMessage('"Hello" from React Native!'); | ||
} | ||
} | ||
|
||
render(): ReactElement<any> { | ||
const {messagesReceivedFromWebView, message} = this.state; | ||
|
||
return ( | ||
<View style={[styles.container, { height: 200 }]}> | ||
<View style={styles.container}> | ||
<Text>Messages received from web view: {messagesReceivedFromWebView}</Text> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. react/no-string-refs: Using this.refs is deprecated. |
||
<Text>{message || '(No message)'}</Text> | ||
<View style={styles.buttons}> | ||
<Button text="Send Message to Web View" enabled onPress={this.postMessage} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. property |
||
</View> | ||
</View> | ||
<View style={styles.container}> | ||
<WebView | ||
ref={webview => { this.webview = webview; }} | ||
style={{ | ||
backgroundColor: BGWASH, | ||
height: 100, | ||
}} | ||
source={require('./messagingtest.html')} | ||
onMessage={this.onMessage} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. property |
||
/> | ||
</View> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
var styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
|
@@ -391,5 +438,9 @@ exports.examples = [ | |
/> | ||
); | ||
} | ||
}, | ||
{ | ||
title: 'Mesaging Test', | ||
render(): ReactElement<any> { return <MessagingTest />; } | ||
} | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Messaging Test</title> | ||
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | ||
<meta name="viewport" content="width=320, user-scalable=no"> | ||
</head> | ||
<body> | ||
<p>Messages recieved from React Native: 0</p> | ||
<p>(No messages)</p> | ||
<button type="button"> | ||
Send message to React Native | ||
</button> | ||
</body> | ||
<script> | ||
var messagesReceivedFromReactNative = 0; | ||
document.addEventListener('message', function(e) { | ||
messagesReceivedFromReactNative += 1; | ||
document.getElementsByTagName('p')[0].innerHTML = | ||
'Messages recieved from React Native: ' + messagesReceivedFromReactNative; | ||
document.getElementsByTagName('p')[1].innerHTML = e.data; | ||
}); | ||
|
||
document.getElementsByTagName('button')[0].addEventListener('click', function() { | ||
window.postMessage('"Hello" from the web view'); | ||
}); | ||
</script> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curly: Expected { after 'if' condition.