skip to Main Content

Currently, I am using client side rendering for a page that fetches data using api and then displays this raw data.

'use client'

import React, { useState, useEffect } from 'react'
import axios from 'axios';

const SolarPanels = () => {
    const [titles, setTitles] = useState([]);

    useEffect(() => {
        axios.get(`/api/panelUpdate?cb=${Date.now()}`, {
            headers: {
                'Cache-Control': 'no-cache',   // Prevents axios from caching
                'Pragma': 'no-cache',
                'Expires': '0'
            }
        })
            .then((response) => { setTitles(response.data.documents[0].productDetails) })
            .catch((error) => { console.error(error.message) });
    }, []);

    return (
        <div>
            <h1>Panels data</h1>
            <p>{titles.map((element) => {
                return <div>{JSON.stringify(element)}</div>
            })}</p>
        </div>
    );
}

export default SolarPanels

I’ve read that its better to perform Client side rendering instead of Server side, so I newant to change this code in order to do that. I’m not planning to make use of hooks other than the one just used.

Also, please tell whether its worth it to convert this to client side rendering?

2

Answers


  1. In Next.js, you can make Client Components but they will be rendered on server-side only. However, since you are using useEffect, it might be possible that you are overkilling it with server-side rendering. You can switch to react, if possible, if you want to do client-side rendering.

    Not using Nextjs for server-side rendering seems a bit strange. You might want to re-consider if you want client side rendering or SSR.

    Read this for more info

    Login or Signup to reply.
  2. You always prefer server component for reasons explained in NextJS doc (that’s why they use it by default)

    https://nextjs.org/docs/app/building-your-application/rendering/server-components

    • Hide sensitive data and logic
    • Better performance in fetching data
    • Less code ships to client-side
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search