This.props.onPress is not a function.

I have HomePage.jsone that contains the About us button , and when I click on this button I want to show AboutUs.js. HomePage.jsdisplays correctly, however, when I click on the button, it gives me the following error: this.props.onPress is not a function. (In 'this.props.onPress (e)', 'this.props.onPress' is an instance of Object)

I have App.js

import React, { Component } from 'react';
import { StackNavigator } from 'react-navigation';
import HomePage from './HomePage';
import AboutUs from './AboutUs';


const App = StackNavigator({
    HomePage: {
        screen: HomePage,
        navigationOptions: {
            header: () => null
        }
    },
    AboutUs: { 
      screen: AboutUs,
        navigationOptions: {
          header: () => null
        }
     }
});

export default App;

Then I have a reusable component Button.js:

import React from 'react';
import { Text, TouchableOpacity } from 'react-native';

const Button = ({ onPress, children }) => {
    return (
        <TouchableOpacity onPress={onPress}>
            <Text>{children}</Text>
        </TouchableOpacity>
    );
};

export { Button };

HomePage.jswhich represents the Button component. When I click it, I get the above error

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { Button } from './Button.js';
import AboutUs from './AboutUs';

class HomePage extends Component{

  render() {
    const { navigate } = this.props.navigation;
    return(
      <View>
        <Button
          onPress={() => navigate('AboutUs'), { name: 'About Us' }}
          >About Us</Button>
      </View>
    )
  }
}

export default HomePage;

AboutUs.js

import React, { Component } from 'react';
import { Text, View } from 'react-native';

class AboutUs extends Component {
    render() {
        return (
      <View>
            <Text>About us!</Text>
      </View>
        );
    }
}

export default AboutUs;
+6
source share
2 answers

This is not a valid function.

onPress={() => navigate('AboutUs'), { name: 'About Us' }}

, { name: 'About Us' } ref Button

+7

: "this.props.onPress " :

onPress={this.props.incrementCount()}

:

onPress={ () => this.props.incrementCount()}

.

0

Source: https://habr.com/ru/post/1689166/


All Articles