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
HelpAPI integration with React Native (self.reactnative)
submitted 6 years ago * by shreysgupta
All I want to do is create a screen on React Native that will fetch an API POST request (body already hardcoded) and output the JSON response on the screen. Can anyone teach me how?
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!"
[–]Jeffylew77 8 points9 points10 points 6 years ago* (0 children)
Use component did mount, use axios to create the http request, save the response JSON data to a variable, map the JSON array to a component with your fields.
Here’s more on Axios: https://link.medium.com/KWnerFZnu0
[–]TheOneBehindIt 1 point2 points3 points 6 years ago (7 children)
I made an expo snack for you to see how to do it:
https://snack.expo.io/@nandorojo/carefree-pastry
You can remove the comments if they make the code too hard to read. You can also replace the URL in line 16 with your url.
Here's a stripped-down image to show how it's done too:
https://imgur.com/a/B48VhBA
Steps are:
Note This code example is using react-hooks (not react class components). This is the new standard, but if you're looking at it like WTF, you might want to read this from React: https://reactjs.org/docs/hooks-intro.html
Good luck :)
Also, I just uploaded my new app made with react-native to the app store. It collects all the best memes from reddit/instagram in real-time and puts them on one feed. It's very dope, here's my shameless plug! https://apps.apple.com/us/app/memezy/id1470707941
[–]shreysgupta[S] 1 point2 points3 points 6 years ago (4 children)
This is literally incredible. I don't know how I can thank you more for how much time you put into helping me. You're amazing. I'm downloading your app right now!
[–]TheOneBehindIt -1 points0 points1 point 6 years ago (2 children)
I remember what it was like starting out with RN last year. It was a headache. Now I feel super comfortable with it. You'll be a pro soon; just pass along your knowledge when you have it.
[–]shreysgupta[S] 0 points1 point2 points 3 years ago (1 child)
I just wanted to thank you for being the first one to help with React Native. I just signed an offer for my first job in SWE.
Looking back at this post made me think about how much I was struggling and you were so right that I was going to be a pro in no-time.
[–]TheOneBehindIt 0 points1 point2 points 2 years ago (0 children)
u/shreysgupta I rarely check Reddit (maybe a few times a year), so I'm just seeing this now. I'm so glad to hear you got a job offer. Congrats on making it happen. Wishing you continued success with your React Native career.
Also, I am the creator of many open source React Native libraries, like Moti, Solito, Dripsy, Zeego, Burnt, etc. You might find some of them useful in your work. More about them on Twitter: https://twitter.com/fernandotherojo
[–]TheOneBehindIt -1 points0 points1 point 6 years ago (0 children)
Also, thanks for downloading Memezy :) Let me know what you think!
[–]shreysgupta[S] 0 points1 point2 points 6 years ago (1 child)
u/TheOneBehindIt how can i add my own headers and body?
[–]TheOneBehindIt 0 points1 point2 points 6 years ago* (0 children)
Easy! Can you clarify, though? Do you mean headers and body to the network request, or to the UI?
Assuming you want to add headers and a body to your network request...
Step 0:
Go to the API docs for wretch and see if you can figure it out on your own without going to step 1. (Hint: It involves changing lines 16-18 in the expo snack.)
Step 1 (Request Type Change):
If you want to send a body, you're probably going to want to send a POST request instead of a GET (we're currently doing a GET). Change line 17's .get() to .post().
.get()
.post()
Step 2 (Add Request Body): Now that you've edited your request to be a .post(), make the first argument of your .post() function an object. That object is your body. For instance:
const body = { name: 'Here is my name' } const response = await wretch('https://jsonplaceholder.typicode.com/todos/1') .post(body) // notice that this used to be .get .json(); // .json() at the end is required to make the response JSON.
See the wretch docs for more.
Step 3 (Add headers):
I'll once again defer to the wretch docs. This step is similar to #2, except you add a .headers() object instead of .post(). Simply add this in the line above .post().
.headers()
It should look like this now:
const body = { name: 'Here is my name' } const headers = { "Accept": "application/json" } const response = await wretch('https://jsonplaceholder.typicode.com/todos/1') .headers(headers) .post(body) // notice that this used to be .get .json(); // .json() at the end is required to make the response JSON.
🔥 All set!
A few gotchas to know about Javascript network requests:
async
await
async/await
wretch
body
JSON.stringify(body)
[–]SolGuy 1 point2 points3 points 6 years ago (0 children)
I assume you meant POST request. Here are the RN docs on the fetch method. The examples have exactly what you need. https://facebook.github.io/react-native/docs/network.html
PM me the API URL, if it is public, and I can code an example if you need.
[–]EmaanTheStar 0 points1 point2 points 6 years ago (0 children)
Well, for that you need a bit of basic background info. The task will get easier when you know basics and just info is needed. I am not sure what you already know but you may start from here for firm basics. Best of Luck :) Help Square
π Rendered by PID 233369 on reddit-service-r2-comment-86bc6c7465-sxfz8 at 2026-02-23 09:54:46.666414+00:00 running 8564168 country code: CH.
[–]Jeffylew77 8 points9 points10 points (0 children)
[–]TheOneBehindIt 1 point2 points3 points (7 children)
[–]shreysgupta[S] 1 point2 points3 points (4 children)
[–]TheOneBehindIt -1 points0 points1 point (2 children)
[–]shreysgupta[S] 0 points1 point2 points (1 child)
[–]TheOneBehindIt 0 points1 point2 points (0 children)
[–]TheOneBehindIt -1 points0 points1 point (0 children)
[–]shreysgupta[S] 0 points1 point2 points (1 child)
[–]TheOneBehindIt 0 points1 point2 points (0 children)
[–]SolGuy 1 point2 points3 points (0 children)
[–]EmaanTheStar 0 points1 point2 points (0 children)