首页 > 解决方案 > 条件 jsx 元素 React native

问题描述

我尝试在加载 webview 时显示文本,但 ActivityIndi​​cator 没有消失。

我试图创建一个带有布尔变量的钩子,该变量在加载 webview 时会发生变化,然后它应该显示文本“你好,我是你的猫!” 在第一个条件。


import React,{useState} from 'react';
import { Text, StyleSheet, View, ActivityIndicator } from 'react-native';
import { WebView } from 'react-native-webview';


const Cat = () => {
  return (
    <Cat />
  );
}
const Cafe = () => {
  const [ok, setOk] = useState(false);
  return (
      ok ?
      
    <Text>Hello, I am your cat!</Text>
      :
    <View style={styles.container}>
      <View style={{height: 0, width: 0}}>
        <WebView
          source={{uri: "https://www.google.com/"}}
          onLoad={() => {
            setOk(true)
            console.log(ok);
          }}
        />
      </View>
      <ActivityIndicator size="large" color="#E60081"/>
    </View>
);
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#000',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default Cafe;

标签: javascriptreactjsreact-nativewebviewjsx

解决方案


您正在传递对您的onLoad函数的引用。试试下面的代码。

<WebView
  source={{uri: "https://www.google.com/"}}
  onLoad={setOk(true)}
/>

推荐阅读