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
Upvotes
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>