简体   繁体   English

在 class 组件 react-native 中声明 const

[英]declaring const in class component react-native

I'm trying to making network error screen, in case lost network I want to show a screen with message about network issue,我正在尝试制作网络错误屏幕,以防网络丢失我想显示一个屏幕,其中包含有关网络问题的消息,

before adding some extra lines of code constructor() and Check_Internet() function my code was working well.在添加一些额外的代码行之前, constructor()Check_Internet() function 我的代码运行良好。 I'm accessing const store but there is a我正在访问const 商店,但有一个

TransformError SyntaxError: : unexpected token at line -> const store = useStore(); TransformError SyntaxError: :行中的意外标记-> const store = useStore();

don't know somehow,I'm not able to figure it out whats going on there.不知何故,我无法弄清楚那里发生了什么。

maybe I'm trying to add const in class thats why its appearing也许我正在尝试在 class 中添加 const 这就是它出现的原因

App.js应用程序.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",
  }
});

Convert your component from a class based component to a function based component to use hooks将您的组件从基于 class 的组件转换为基于 function 的组件以使用挂钩

Currently you can't use Hooks inside a class components Read More on the official react docs目前你不能在 class 组件中使用 Hooks 阅读更多关于官方反应文档

Class components cannot use React hooks. Class 组件不能使用 React 钩子。 Best solution is to convert your component from class to functional.最佳解决方案是将您的组件从 class 转换为功能性组件。

There may be another solution to achieve the same result in a class component but if you wish to use hooks, it must be a functional component.可能有另一种解决方案可以在 class 组件中实现相同的结果,但如果您希望使用挂钩,它必须是功能组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM