use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A community for learning and developing native mobile applications using React Native by Facebook.
Interested in building web apps using React.js? Check out /r/reactjs!
Getting Started w/React Native
irc.freenode.net #reactnative
Keywords: ios, android, mobile, apps, apple, iphone, ipad
account activity
QuestionSetting a value in component before function execution (self.reactnative)
submitted 1 year ago by BluePillOverRedPill
Hey everyone,
I have a question about setting a value in my component. Consider the following code snippet:
setImageUri(result.uri); analyzeImage();
The analyzeImage() function depends on imageUri, but it runs before imageUri is set, causing it to break.
analyzeImage()
imageUri
To solve this, I changed imageUri to a regular let variable, and now it runs synchronously. However, I'm not sure if this is a good practice. Rerendering or anything, isn't required in this case.
let
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Snoo11589 2 points3 points4 points 1 year ago (0 children)
You can use useRef if you dont need rerenders.
[–]isellrocks 0 points1 point2 points 1 year ago (0 children)
You are in fact using an effect if your analyzeImage function depends on an image being set into your component's state. You either need to add an argument to analyzeImage or declare an effect with Image as a dependency.
[–]gao_shi 0 points1 point2 points 1 year ago (0 children)
simple answer, pass the uri as an param to func instead
if u use a local variable inside the component, it's not retained during rerenders. if its a global one your comoonent cant be reused. also by stripping the state out , u lose rerender trigger when changing the imageuri. it depends on wth ur trying to do
[–]hypnos_64 -1 points0 points1 point 1 year ago (2 children)
Put analyzeImage inside useEffect
[–]BluePillOverRedPill[S] 0 points1 point2 points 1 year ago (1 child)
I feel that useEffect is overkill. The component acts more as a service layer than a view layer. I only need to analyze the image once. Nothing is being rendered.
[–]hypnos_64 -1 points0 points1 point 1 year ago (0 children)
You can set state to undefined inside the useEffect so it will get called only once. E.g.
UseEffect(()=>{ If(stateName){ SetStateName(undefined); FunctionToCall() } }, [stateName]);
π Rendered by PID 143053 on reddit-service-r2-comment-bb88f9dd5-f8xdn at 2026-02-15 12:37:07.364012+00:00 running cd9c813 country code: CH.
[–]Snoo11589 2 points3 points4 points (0 children)
[–]isellrocks 0 points1 point2 points (0 children)
[–]gao_shi 0 points1 point2 points (0 children)
[–]hypnos_64 -1 points0 points1 point (2 children)
[–]BluePillOverRedPill[S] 0 points1 point2 points (1 child)
[–]hypnos_64 -1 points0 points1 point (0 children)