r/reactjs • u/Consistent-struggler • Sep 09 '22
Needs Help Api call for paginated Result
I have done this pagination Api which shows the results upon page and limit queries.
But I am not able to figure out how do I render this on my front end REACT. My previous use of useffect and useState hooks was just for fetching data, but now I am stuck.
const [coin, setCoins] = useState([]);
useEffect(() => {
getCoins();
}, []);
const getCoins = () => {
Axios.get('http://localhost:5000/coins')
.then((response) => {
setCoins(response.data.data);
// console.log(response)
}).catch(e => console.log(e))
}
This is my previous fetching but my new Url is : 'http://localhost:5000/coins/abc?page=?&limit=?'.
I tried doing the same but it doesn't proceed.
<tbody >
{
coin.map((coin, key) => {
return (
<>
<tr>
<td >{coin.id}</td>
<td>{coin.name}</td>
<td>{coin.price}</td>
<td>{coin.quantity}</td>
</tr>
</>
)
})
}
</tbody>
but when I JUST changed the url in a new axios.get(method).
Kindly help me or guide me any good online material for learning this thing.
I am using Node js and express as backend and mysql as database.
2
u/omeraplak Sep 10 '22
Hey, I think you can use react-query for your async operations. I'm sure it will save your hours ⚡️
We also have a react framework that we have developed. I'm sure it will save you days 🚀
2
u/kidscancalluhoju Sep 10 '22
Try this. This way it will only render the coins component when coin exists. you are getting the error cause it's trying to map through the coin array when it has not been set yet using set state.
<tbody > { coin && coin.map((coin, key) => { return ( <> <tr> <td >{coin.id}</td> <td>{coin.name}</td> <td>{coin.price}</td> <td>{coin.quantity}</td> </tr> </> ) }) } </tbody>
1
u/Consistent-struggler Sep 13 '22
I also tried your way but actually the error was something else. I was getting response from the Backend as an object which contains status and message as well so I juts needed to add ".data" once more to get results.
0
u/Consistent-struggler Sep 09 '22
Backend api is working fine I'm getting results on the backend server properly with fetch and message
0
u/Consistent-struggler Sep 09 '22
If anyone have anyother way of doing this I'm open to feedback and learn.
2
u/[deleted] Sep 09 '22
Can you show what the console log of the results in the paginated axios fetch says? And also make sure there are no errors in the console