简体   繁体   English

React Native Render 方法语法错误要分号

[英]React Native Render method syntax error wants a semicolon

I am trying to use a simple function component to render a popup.我正在尝试使用一个简单的 function 组件来呈现弹出窗口。 I was following react-native-modul github page.我正在关注 react-native-modul github 页面。https://github.com/react-native-community/react-native-modalhttps://github.com/react-native-community/react-native-modal

For some reason I keep getting an error next to render() saying I need a;出于某种原因,我在 render() 旁边不断收到一个错误,说我需要一个; when in all my other code it is set up the same way.在我所有其他代码中,它的设置方式相同。 Is something else causing this?是其他原因造成的吗? enter image description here在此处输入图像描述

import React, {useState} from 'react';
import {Button, Text, View} from 'react-native';
import Modal from 'react-native-modal';

function Popup() {
  const [isModalVisible, setModalVisible] = useState(false);

  const toggleModal = () => {
    setModalVisible(!isModalVisible);
  };

  render() {
    return (
      <View style={{flex: 1}}>
        <Button title="Show modal" onPress={toggleModal} />

        <Modal isVisible={isModalVisible}>
          <View style={{flex: 1}}>
            <Text>Hello!</Text>

            <Button title="Hide modal" onPress={toggleModal} />
          </View>
        </Modal>
      </View>
    );
  }
}

export default Popup;

You can only use render in a class component.您只能在 class 组件中使用render

import React, {useState} from 'react';
import {Button, Text, View} from 'react-native';
import Modal from 'react-native-modal';

function Popup() {
  const [isModalVisible, setModalVisible] = useState(false);

  const toggleModal = () => {
    setModalVisible(!isModalVisible);
  };

    return (
      <View style={{flex: 1}}>
        <Button title="Show modal" onPress={toggleModal} />

        <Modal isVisible={isModalVisible}>
          <View style={{flex: 1}}>
            <Text>Hello!</Text>

            <Button title="Hide modal" onPress={toggleModal} />
          </View>
        </Modal>
      </View>
    );
  }

export default Popup;

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

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