React native 单击按钮时响应本机更改webview源
我只想在单击webview外部的按钮后,将webview组件的源更改为不同的url。我被吓坏了,没有比这更简单的方法了。我被告知要使用“refs”,但考虑到我的特殊情况,我甚至不确定这是否有效 为了表达我的期望,我加入了一些web jsReact native 单击按钮时响应本机更改webview源,react-native,React Native,我只想在单击webview外部的按钮后,将webview组件的源更改为不同的url。我被吓坏了,没有比这更简单的方法了。我被告知要使用“refs”,但考虑到我的特殊情况,我甚至不确定这是否有效 为了表达我的期望,我加入了一些web js doAppNotification() { // some objc code is run // receiveAppNotification is called } receiveAppNotification() { let wv
doAppNotification() {
// some objc code is run
// receiveAppNotification is called
}
receiveAppNotification() {
let wv = document.getElementById("webview"); // !! how can I do something like this in react native??
wv.source="http://some.other.site";
}
...
<Webview
id="webview"
source={{uri: "https://www.google.com"}}
/>
<Button
onClick={doAppNotification}
/>
您可以轻松地使用组件状态来实现所需的行为。更改状态将触发组件重新渲染,并打开您设置的新uri
这太棒了!在收到来自iOS库的回调后,我正在更改状态,在新范围内,this关键字绑定到专用的WorkerGlobalScope,而不是App。我所做的是通过使用全局变量从构造函数中存储对App的引用。我以后得研究一个更好的解决办法。
export default MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
url: 'https://defaulturl'
}
}
someEvent = (e) => {
this.setState({ url: 'https://toanewurl' });
}
render(){
const { url } = this.state;
return(
<React.Fragment>
<WebView source={{uri: url}} />
<Button onClick={this.someEvent} />
</React.Fragment>
)
}
}