Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
React native call functions and change states from a component

So I currently have a function that renders a component NewButton, in that component, how do I call the function toggleModalVisibility, which is defined outside of that component? That function will change many states and call other functions, and change rendering as well.

import React, {  useState, useEffect} from 'react'
import {Button, View, Text, StyleSheet,TextInput,ScrollView, Modal} from 'react-native'
import { BottomSheet } from 'react-native-btr';
import NewButton from './NewButton'


export default function CardFolder({navigation, navigation: {setOptions}}){

    const [visible, setVisible] = useState(false);
    //many other states
 
    const toggleModalVisibility = () => {
        //changes states, calls other functions, change rendering, Toggle bottomSheet
    };

    return(
        < >
        <Modal> modal containing a text field, change states </Modal>
        <BottomSheet>contains other options, upload something to db</BottomSheet>
        <View style = {{flex:1}}>

        <NewButton style = {{bottom: 50}} />

        </View>
        </>  
    )      
}  

The Component:

import {View, Button} from 'react-native'

export default class NewButton extends React.Component {
 
        return(
            <View style = {[styles.container,this.props.style]}>
                //how do I call the functon toggleModalVisibility here?
                <Button onPress={toggleModalVisibility}/>
            </View>
        )
    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you can add it as a prop to NewButton Component

<NewButton style={{bottom: 50}} toggleModalVisibility={toggleModalVisibility} />

and the call it like

<Button onPress={this.props.toggleModalVisibility}/>
about 4 years ago · Juan Pablo Isaza Report

0

As @Krosf suggested you, you should pass the function by props and then call the function when Button is pressed, in your case you are using class component, so you should use the "this" keyword in order to access your "NewButton props"

like this:

<Button onPress={ this.props.toggleModalVisibility } />
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!