Hi there, I'm fairly new to working with next.js and would greatly appreciate some assistance.
I am trying to figure out how to fetch data from an API upon clicking a button in next.js on the server side.
I understand that using onClick directly is not possible without making it client-side, but I want to avoid doing that.
Below is the code snippet where I am retrieving data straight from the server:
import Button from "@/components/button/Button";
import { get } from "@/services/ApiService";
import React from "react";
const getData = async() => {
console.log("data from server")
const response = await get(`posts`)
return response?.data
}
async function About() {
const data = await getData();
return (
<div>
Welcome to About test
<Button data="posts" />
{data &&
data.map((item, index) => {
return (
<div key={index}>
<div>{item.id}</div>
<div>{item.title}</div>
</div>
);
})}
</div>
);
}
export default About;