如何为webview设置动态URL?

问题描述 投票:0回答:1

我正在做一个快速原型,还不熟悉react。

我创建了一个应用程序,我想根据应用程序打开时传递的参数(链接)动态地注入一个URL到我的webview(react-native-webview)。

import {WebView} from 'react-native-webview';
import {Linking} from 'react-native';

class MyWeb extends Component {

    state = {

    }
  componentDidMount() {
    Linking.addEventListener('url', this.handleOpenURL);
  }

  componentWillUnmount() {
    Linking.removeEventListener('url', this.handleOpenURL);
  }

  handleOpenURL = (event) => {
    console.log(event.url);
    state = {
        url: ==> I will define it

      };


    //const route = e.url.replace(/.*?:\/\//g, '');
    //console.log(route);
  };

  render() {
    return (
      <WebView
        onLoadStart={(navState) =>
          this.setState({url: this.state.url})
        }
        source={{
          uri: this.state.url,
        }}
        style={{marginTop: 20}}
      />
    );
  }
}

export default MyWeb;

我在onLoadStart上做的事情似乎没有用,但说实话,我很想念这个逻辑......

先谢谢你的建议。

react-native react-native-navigation
1个回答
1
投票

你需要使用 Deep-linking 从反应式导航

https:/reactnavigation.orgdocsdeep-link。

然后用 prefix 传给 WebView.

© www.soinside.com 2019 - 2024. All rights reserved.