如何在React Native中更改视图内容

oknwwptz  于 2022-11-30  发布在  React
关注(0)|答案(2)|浏览(162)

我一直在尝试替换React Native中的视图,但没有成功。每当我尝试<TouchableOpacity onPress={() => {handleChangeMyView();}}>时,应用程序都会关闭,没有错误:
我做错了什么?我该怎么做?
提前感谢大家。

import React, {
  useState
} from 'react';

import {
  SafeAreaView,
  View,
} from 'react-native';

import MyInitialView from './MyInitialView';

const SiteContainer = () => {
  let MyDynamicView = () => {
    return (
      <View></View>
    );
  };

  const [MyDynamicViewArea, setMyDynamicViewArea] = useState(MyInitialView);

  const handleChangeMyView = () => {
    setMyDynamicViewArea(MyDynamicView);
  };

  return (
    <SafeAreaView style={styles.siteContainer}>
      {MyDynamicViewArea}
      <TouchableOpacity onPress={() => {handleChagnStaceMyView();}}>
        <View>
          <FontAwesome name="quote-left"></FontAwesome>
        </View>
      </TouchableOpacity>
    </SafeAreaView>
  );
};

export default SiteContainer;

我的初始视图

import React from 'react';

import {
  View
} from 'react-native';

export default function MyInitialView() {
  return (
    <View></View>
  );
}
9vw9lbht

9vw9lbht1#

您正在调用错误的函数<TouchableOpacity onPress={() => {handleChagnStaceMyView();}}>,请将其更改为<TouchableOpacity onPress={() => {handleChangeMyView();}}>
祝你好运
我想知道你没有得到像handleChagnStaceMyView is not defined这样的错误

db2dz4w8

db2dz4w82#

TouchableOpacity的onPress中的函数名称不正确。请更改此名称

onPress={() => {handleChagnStaceMyView();}}

onPress={() => {handleChangeMyView();}}

相关问题