#Is it possible to re render part of a page in astro?

1 messages · Page 1 of 1 (latest)

shadow nimbus
#
---
import Card from "../components/team/Card.astro";
import Home from "../layouts/Home.astro";
import { readMembers } from "../util/json";

let members = readMembers();
---

<script>
    document.querySelector("#test")?.addEventListener("click", () => {
        members.pop();
    });
</script>

<Home title="p">
    <h1 class="text-center mt-16 text-6xl font-bold">The Team</h1>
    <button id="test" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
        Button
    </button>
    <div
        class="grid sm:grid-cols-2 md:grid-cols-3 xl:grid-cols-5 gap-4 mx-4 my-4 mt-16"
    >
        {
            members.map((member) => (
                <Card
                    name={member.name.first + " " + member.name.last}
                    grade={member.grade}
                    roles={member.roles}
                    image={member.image}
                >
                    {member.tidbit}
                </Card>
            ))
        }
    </div>
</Home>

When the button is pressed it should remove the last element of the array. However, it errors in the console. Furthermore, if I get it to successfully remove will the ui update accordingly, or am I going to need to find a way to re render all of the cards

opal karmaBOT
#

Hi @shadow nimbus. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Astro is a front-end framework that allows you to build websites using JavaScript, but it emphasizes minimized JavaScript usage and optimized loading times. It works a lot like a static site generator, rendering your entire site to static HTML during the build. This means that Astro doesn't have built-in reactivity or a virtual DOM like some other JavaScript frameworks such as React or Vue.

In your case, you're trying to manipulate the DOM directly using JavaScript inside an Astro component. This approach won't work as expected because Astro components are server-rendered and don't have built-in reactivity.

If you need to handle dynamic behavior like removing an item from an array and updating the UI accordingly, you might want to consider using a UI component from a JavaScript framework that supports reactivity, such as React, Svelte, or Vue, and then import that component into your Astro component.

Here's an example of how you can use a React component in an Astro file, as shown in the Astro documentation (https://docs.astro.build/en/guides/markdown-content/):

---
layout: ../layouts/BaseLayout.astro
title: About me
---
import Button from '../components/Button.astro';
import ReactCounter from '../components/ReactCounter.jsx';

I live on **Mars** but feel free to <Button title="Contact me" />.

Here is my counter component, working in MDX:
<ReactCounter client:load />

In this example, ReactCounter is a React component that can handle dynamic behavior. You would need to add the client:load directive to your component to tell Astro to load it in the browser where it can have access to the DOM and handle dynamic behavior.

#

Remember, Astro's goal is to ship as little JavaScript as possible, so use client-side interactivity sparingly.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: