Home > other >  How to change View content in React Native
How to change View content in React Native

Time:11-30

I've been trying to replace a view in React Native, but to no success. The app closes without errors whenever I try <TouchableOpacity onPress={() => {handleChangeMyView();}}> :

What am I doing wrong? How can I make it work?

Thank you all in advance.

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;

MyInitialView :

import React from 'react';

import {
  View
} from 'react-native';

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

CodePudding user response:

You have incorrect name of function in onPress of TouchableOpacity. Change this

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

to

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

CodePudding user response:

you are calling wrong function <TouchableOpacity onPress={() => {handleChagnStaceMyView();}}> make it right as <TouchableOpacity onPress={() => {handleChangeMyView();}}>

good luck

I wonder you are not getting error like handleChagnStaceMyView is not defined

  • Related