Next.js Discord

Discord Forum

Update data in client component

Answered
American Crocodile posted this in #help-forum
Open in Discord
American CrocodileOP
My client component display a table with alerts. I want the web page to be opened and automatically refresh on new alerts. I'm using next 14, app router. I didn't find a way to like "subscribe" to the data so the server say when there is a new data. I looked for automatic refresh on the client, but i only found ways using /api (i guess it was good before app router, but seems strange to add an api just for that).
Btw if I understand well I'm doing a server side rendering.
Do you have any tips to do the automatic refresh ? (can server actions be used for that ??)
Answered by B33fb0n3
you can use clientside fetching libraries like react query or srw. I prefer react query. With that I can do all the fetching stuff you mentioned and of course also the background refetching or even the intervall fetching
View full answer

20 Replies

American CrocodileOP
for now, this is my server component
"use server"

import AlertTable from "@/app/ui/table/alertTable";
import { fetchUrgentAlerts } from "@/app/lib/data";
import { AlertWithMatch } from "@/app/lib/definitions";
import { Container } from "@chakra-ui/react";




export default async function UnreadAlert() {

    const alerts: AlertWithMatch[] = await fetchUrgentAlerts();


    return (
        <Container maxW="100%" mx={0} w={"100%"} mt={8} borderWidth={4} borderColor={"background.100"} p={8} borderRadius={8}>
                <AlertTable title={"Unread alerts"}
                                captions={["Author", "Title", "Content", "Date"]}
                                data={alerts}/>  
        </Container>
    );
    }
and this is my client component displaying the data
"use client"

// Chakra imports
import {
  Flex,
  Table,
  Tbody,
  Text,
  Th,
  Thead,
  Tr,
  useColorModeValue,
} from "@chakra-ui/react";
// Custom components
import Card from "../purify/components/Card/Card";
import CardBody from "../purify/components/Card/CardBody";
import CardHeader from "../purify/components/Card/CardHeader";
// Types

import React from "react";
import AlertTableRow from "./TablesAlertRow";
import { AlertWithMatch } from "@/app/lib/definitions";

const AlertTable = ({ title, captions, data } : { title: string, captions: string[], data: AlertWithMatch[] }) => {
  const textColor = useColorModeValue("gray.700", "white");
  return (
    <Card  overflowX={{ sm: "scroll", xl: "hidden" }}>
      <CardHeader p='0px 0px 22px 0px'>
        <Flex direction='column'>
          <Text fontSize='lg' color={textColor} fontWeight='bold' pb='.5rem'>
            {title}
          </Text>
        </Flex>
      </CardHeader>
      <CardBody>
        <Table variant='simple' color={textColor}>
          <Thead>
            <Tr my='.8rem' pl='0px'>
              {captions.map((caption, idx) => {
                return (
                  <Th color='gray.400' key={idx} ps={idx === 0 ? "0px" : undefined}>
                    {caption}
                  </Th>
                );
              })}
            </Tr>
          </Thead>
          <Tbody>
            {data.map((alert) => {
              return (
                <AlertTableRow
                  key={alert.id}
                  alert={alert}
                    
                />
              );
            })}
          </Tbody>
        </Table>
      </CardBody>
    </Card>
  );
};

export default AlertTable;
Answer
@B33fb0n3 you can use clientside fetching libraries like react query or srw. I prefer react query. With that I can do all the fetching stuff you mentioned and of course also the background refetching or even the intervall fetching
American CrocodileOP
okay thanks, i'm gonna look further into it. And is there a way to deploy as dev with hot reload, but having the "cache" like in prod ? To see if the data doesn't refresh on page refreshing
@American Crocodile okay thanks, i'm gonna look further into it. And is there a way to deploy as dev with hot reload, but having the "cache" like in prod ? To see if the data doesn't refresh on page refreshing
yes, you can start directly in prod mode when using next build and next start after that. Then you can do nearly the same like you do in dev mode, but you have prod cache behavior
@B33fb0n3 yes, you can start directly in prod mode when using next build and next start after that. Then you can do nearly the same like you do in dev mode, but you have prod cache behavior
American CrocodileOP
there is hot reaload after a next start ????? I assumed not, but should have checked. Thank you :)
@American Crocodile there is hot reaload after a next start ????? I assumed not, but should have checked. Thank you :)
I am not quite sure if there is, but ... it runs on localhost:3000... so test it
American CrocodileOP
yes, gonna do it
@American Crocodile yes, gonna do it
when will you test it?
@B33fb0n3 when will you test it?
American CrocodileOP
It doesn't rebuild. It makes sense
So it's not possible to do it like that
@American Crocodile So it's not possible to do it like that
oh ok. Yea, then test in production the caching for production and develope in dev env.
American CrocodileOP
yeah, it's not the better way to dev, but i'll test like that :p
@American Crocodile yeah, it's not the better way to dev, but i'll test like that :p
alright 👍 Is you issue solved like that?
American CrocodileOP
yeah kinda, I fixed it so first try so didn't really need it tbh, but thank you :)
American CrocodileOP
So sorry, thx
np