我正在try 制作网络错误画面,以防丢失我想要的网络 要显示带有有关网络问题的消息的屏幕,

在添加一些额外的代码行constructor()Check_Internet()函数之前,我的代码运行良好. 我正在拨打const store,但有一个

TransformError SyntaxError: : unexpected token 在行-->const store = useStore();

不知道怎么回事,我搞不清楚那里发生了什么事.

也许我是想把Const加到课堂上,这就是为什么它会出现

App.js

import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
import { TailwindProvider } from "tailwindcss-react-native";
import { Provider } from "react-redux";
import React, { Component } from 'react'

import Routes from "./routes";
import { useStore } from "./store";

import Lottie from 'lottie-react-native';
import NetInfo from '@react-native-community/netinfo';
import ButtonCstm from "./custom-components/Button";


export default class main extends Component {

  const store = useStore();

  constructor() {
    super();
    this.state = {
      network_status: "",
    }
    this.Check_Internet();

  }

  Check_Internet = async () => {
    await NetInfo.fetch().then(state => {
      console.log("Connection type", state.type);
      console.log("Is Connected", state.isConnected);

      if (state.isConnected == true) {
        this.setState({
          network_status: "online"
        })
      }
      else {
        this.setState({
          network_status: "offline"
        })
      }

    });
  }

  render() {
    if (this.state.network_status == "online") {
      return (
        <TailwindProvider>
          <Provider
            store={store}
          >
            <Routes />
            <StatusBar style="auto" />
            <Text> hey you are online</Text>
          </Provider>
        </TailwindProvider>
      )
    }
    else {
      return (
        <TailwindProvider>
          <Provider store={store} >
            <Routes />
            <StatusBar style="auto" />
            <Lottie style={{ marginBottom: 50, }} source={require('../../assets/animation/no-internet1.json')} autoPlay loop />
            <Text style={styles.txt}> hey you are Offline please check your internet</Text>
            <ButtonCstm
            stylebtn={{
              height: 50,
              width: 200,
              backgroundColor: "rgba(90, 154, 230, 1)",
              borderRadius: 10,
              position: "absolute",
              bottom: 80,

            }}
            title={"Try Again"}
            stylebtntitle={{
              color: colors.black,
              fontWeight: "normal",
              fontSize: 20,
              marginTop: 12,
              textAlign: "center",
              fontFamily: "OpenSans",
            }}
            onPress={this.Check_Internet}
          />
          </Provider>
        </TailwindProvider>
      )
    }
  }
}
const styles = StyleSheet.create({
  txt: {
    fontSize: 20,
    fontWeight: "bold",
  }
});

推荐答案

将组件从基于类的组件转换为基于函数的组件以使用挂钩

目前,您不能在类组件中使用钩子 Read More on the official react docs

Example:

从您的代码中,您可以将组件从基于类转换为基于函数,如下所示:

import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
import { TailwindProvider } from "tailwindcss-react-native";
import { Provider } from "react-redux";
// import useState and useEffect hooks from react
import React, { Component, useState, useEffect } from 'react'

import Routes from "./routes";
import { useStore } from "./store";

import Lottie from 'lottie-react-native';
import NetInfo from '@react-native-community/netinfo';
import ButtonCstm from "./custom-components/Button";

// convert class main to function main
export default function main () {
  // initialize network_status state here with useState
  const [network_status, set_network_status] = useState("");

  const store = useStore();
  
  // use useEffect to run Check_Internet when the component mounts
  useEffect(() => {
    Check_Internet()
  }, [])

  Check_Internet = async () => {
    await NetInfo.fetch().then(state => {
      console.log("Connection type", state.type);
      console.log("Is Connected", state.isConnected);

      if (state.isConnected == true) {
        // set state with set_network_status
        set_network_status("online");
      }
      else {
        // set state with set_network_status
        set_network_status("offline");
      }
    });
  }

  // remove render and return raw jsx
  if (network_status == "online") {
    return (
      <TailwindProvider>
        <Provider
          store={store}
        >
          <Routes />
          <StatusBar style="auto" />
          <Text> hey you are online</Text>
        </Provider>
      </TailwindProvider>
    )
  }
  else {
    return (
      <TailwindProvider>
        <Provider store={store} >
          <Routes />
          <StatusBar style="auto" />
          <Lottie style={{ marginBottom: 50, }} source={require('../../assets/animation/no-internet1.json')} autoPlay loop />
          <Text style={styles.txt}> hey you are Offline please check your internet</Text>
          <ButtonCstm
          stylebtn={{
            height: 50,
            width: 200,
            backgroundColor: "rgba(90, 154, 230, 1)",
            borderRadius: 10,
            position: "absolute",
            bottom: 80,

          }}
          title={"Try Again"}
          stylebtntitle={{
            color: colors.black,
            fontWeight: "normal",
            fontSize: 20,
            marginTop: 12,
            textAlign: "center",
            fontFamily: "OpenSans",
          }}
          onPress={Check_Internet}
        />
        </Provider>
      </TailwindProvider>
    )
  }
}
const styles = StyleSheet.create({
  txt: {
    fontSize: 20,
    fontWeight: "bold",
  }
});

你可以在useStateuseEffect上阅读更多关于Reaction文档的官方网站

USEFUL LINKS

Javascript相关问答推荐

如何访问Json返回的ASP.NET Core 6中的导航图像属性

使用useup时,React-Redux无法找到Redux上下文值

Klaro与Angular的集成

docx.js:如何在客户端使用文档修补程序

判断表格单元格中是否存在文本框

如何从隐藏/显示中删除其中一个点击?

如何通过使用vanilla JS限制字体大小增加或减少两次来改变字体大小

如何在Angular拖放组件中同步数组?

如何将zoom 变换应用到我的d3力有向图?

SPAN不会在点击时关闭模式,尽管它们可以发送日志(log)等

如何在HTMX提示符中设置默认值?

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

令牌JWT未过期

JS Animate()方法未按预期工作

ngOnChanges仅在第二次调用时才触发

未找到用于 Select 器的元素:in( puppeteer 师错误)

如何用react组件替换dom元素?

使用Java脚本在div中创建新的span标记

Select 所有输入.值

JQuery-无法 Select 使用elementor添加的元素的值