• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

156
Views
How can I pass a value to a function when onPress is pressed in Reactnative?

I'm using Reactnaitve When onPress is triggered, the photosModalControl function is executed and I want to get the value first, and I also want to execute setUploadPhotos. But when I use my code, I get the value, but setUploadPhotos is not executed. How do I fix the code?

this is my code

const photosModalControl = (value: string) => () =>  {
    console.log(value);
    setUploadPhotos(prevState => !prevState);
};


<Pressable 
onPress={photosModalControl("first")}
>
</Pressable>
almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If im not mistaken, shouldn't you set up your function like this:

const someFunction = (variable: type) => { functions stuff}

you doubled up and put:

const someFunction = (variable: type) = () => { functions stuff}

almost 3 years ago · Juan Pablo Isaza Report

0

The way you are passing the function in the onPress will result in that function call on render. Instead use this:

    const photosModalControl = (value: string) => () =>  {
        console.log(value);
        setUploadPhotos(prevState => !prevState);
    };


    <Pressable onPress={()=>photosModalControl("first")}>Button</Pressable>

This way you can pass any number of arguments to your function

almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error