React native 如何设置Web视图的动态URL?

React native 如何设置Web视图的动态URL?,react-native,react-native-navigation,React Native,React Native Navigation,我正在制作一个快速原型,但还不熟悉react 我创建了一个应用程序,我想根据打开应用程序时传递的参数(链接)动态地将URL注入我的webview(react native webview) import {WebView} from 'react-native-webview'; import {Linking} from 'react-native'; class MyWeb extends Component { state = { } componentDidMo

我正在制作一个快速原型,但还不熟悉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;
从'react native WebView'导入{WebView};
从“react native”导入{Linking};
类MyWeb扩展组件{
状态={
}
componentDidMount(){
Linking.addEventListener('url',this.handleOpenURL);
}
组件将卸载(){
Linking.removeEventListener('url',this.handleOpenURL);
}
handleOpenURL=(事件)=>{
日志(event.url);
状态={
url:=>我将定义它
};
//const route=e.url.replace(/.*?:\/\//g',);
//控制台日志(路由);
};
render(){
返回(
this.setState({url:this.state.url})
}
来源={{
uri:this.state.url,
}}
style={{marginTop:20}}
/>
);
}
}
导出默认MyWeb;
我在onLoadStart上所做的似乎毫无用处,但老实说,我错过了逻辑


提前感谢您的建议。

您需要使用react navigation的
深度链接


然后使用
前缀
动态创建路径,并传递到
网络视图

是否尝试使用
this.state.url
包装
网络视图
组件,以便在url更改时完全重新呈现网络视图,而不是尝试刷新url
{this.state.url&&this.setState({url:this.state.url}})source={{uri:this.state.url}/>}
您好,感谢您的快速回复。我确实尝试过在我的handleOpenUrl函数中执行setstate,但是当webview被加载时,URL是空的,即使是通过复制/粘贴您的代码…也许当它加载时,它的值是空的,您是否可以进行条件空检查来更新状态。