React Navigation:如何在状态更改时在选项卡屏幕中重新呈现Webview?

时间:2020-09-03 23:55:10

标签: react-native react-navigation

我有一个Tab屏幕,可实现像这样的react native Webview:

<Tab.Screen name="Home" component={WebViewScreen} initialParams={{
  route: `https://example.com/${lang}`
}} />

WebView加载路线:

<Webview key={key} source={{ uri: props.route.params.route }} />

但是,当lang(通过上下文API传递的变量)被更改时,Webview不会反映对uri的更改,因为initialParams似乎是硬编码的

我尝试了一些相当不合格的方法,但仍然无法正常工作,无论如何,都会刷新每个标签页:

React.useEffect(() => {
  const unsubscribe = props.navigation.addListener('focus', () => {
  
    if (lang === 'de' && url.indexOf('/de/') === -1) {
      Reactotron.log(`Switch to ${lang}`)
      setUrl(url.replace('/en/', '/de/'))
      setKey(key + 1)
    } else if (lang === 'en' && url.indexOf('/en/') === -1) {
      Reactotron.log(`Switch to ${lang}`)
      setUrl(url.replace('/de/', '/en/'))
      setKey(key + 1)
    }
  })
return unsubscribe
}, [props.navigation, lang]);

这个非常基本的问题没有简单的解决方法吗?

0 个答案:

没有答案