On my API.js i have this code to call API JSON.
import { View, Text,Button, FlatList } from 'react-native';
import React, { useState } from 'react';
import axios from 'axios';
export default function APIs() {
const [data, setData] = useState([]);
const baseURL = 'https://jsonplaceholder.typicode.com';
const getAPI = () => {
axios({
method: 'GET',
url: `${baseURL}/posts`,
})
.then(res => console.log(res.data))
.catch(err => console.log(err));
};
}
How can I show response of that, on few pages?
Lets say on Home.js
I want to display "id", then on title I want to display "title".
I mean, how can I make call and display it in the parts on multiple pages, each part on different page?
2
Answers
Make it a hook:
Use in individual pages:
react-doc
If your
<Home />
and<Title />
components have a common ancestor (<App />
in the snippet blow), then this component can fetch the data needed, store it, then send each required piece to the corresponding child component through props.The following snippet showcases a simple example where the
<App />
component fetches the data, then sends the ID of the 1st element to<Home />
and the title to<Title />
.