如何在数据变化时刷新/重新渲染Astro组件?

huangapple go评论160阅读模式
英文:

How do i refresh/rerender an Astro component when data changes?

问题

我有这个简单的组件。当我知道API数据已更改时,如何刷新/重新渲染它?
示例:当我单击仪表板项目之一时,将生成一个包含表单的对话框。当该表单被提交时,API数据将更改,我需要刷新组件。

或者我是否需要使用VUE、React或另一个框架来完成这个任务?

---
import Layout from "../layouts/Layout.astro";
import BrokerDashboardItem from "../components/BrokerDashboardItem.astro";

const response = await fetch(
    "https://api/"
);
const data = await response.json();

---

<Layout title="Broker Dashboard">
<div>
    {
        data.map((item) => (
            <BrokerDashboardItem
                priority="{item.priority}"
                date="{item.date}"
                clientName="{item.clientName}"
                statusText="{item.statusText}"
                statusProgress="{item.statusProgress}"
                title="{item.title}"
            />
        ))
    }
</div>
    <style></style>
</Layout>

请注意,刷新组件的方式取决于您使用的框架或库。如果您正在使用Vue或React等前端框架,通常会有特定的机制来处理组件的刷新和重新渲染。如果您只是使用原生JavaScript和HTML,您可以考虑手动更新组件的数据或状态以触发重新渲染。

英文:

I have this simple component. How do i refresh/rerender it when i know the api data has changed?
Example: When i click one of the dashboard items i will spawn a dialog which contains a form. When that form is submitted the api data will change and i need to refresh the component.

Or do i need VUE, React or another framework to do this?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

---
import Layout from &quot;../layouts/Layout.astro&quot;;
import BrokerDashboardItem from &quot;../components/BrokerDashboardItem.astro&quot;;

const response = await fetch(
    &quot;https://api/&quot;
);
const data = await response.json();

---

&lt;Layout title=&quot;Broker Dashboard&quot;&gt;
&lt;div&gt;
    {
        data.map((item) =&gt; (
            &lt;BrokerDashboardItem
                priority=&quot;{item.priority}&quot;
                date=&quot;{item.date}&quot;
                clientName=&quot;{item.clientName}&quot;
                statusText=&quot;{item.statusText}&quot;
                statusProgress=&quot;{item.statusProgress}&quot;
                title=&quot;{item.title}&quot;
            /&gt;
        ))
    }
&lt;/div&gt;
    &lt;style&gt;&lt;/style&gt;
&lt;/Layout&gt;

<!-- end snippet -->

答案1

得分: 1

Astro组件的渲染逻辑仅存在于服务器上,因此要在客户端上重新渲染,您需要选择以下之一:

  • 使用像Preact、React、Vue、Solid、Svelte等支持在客户端上重新渲染的框架。

  • <script>标签中编写描述如何更新组件的交互性,例如使用自定义元素。

您选择哪种方式主要取决于个人偏好和项目要求。如果您只有少数几个这种更新行为的实例,我可能会选择编写一个自定义元素来处理这些事情。但如果项目需要大量互动,通常使用UI框架将有助于更轻松地构建和维护。

英文:

The Astro component rendering logic is only present on the server, so to re-render on the client you need to choose one of the following

  • Use a framework like Preact, React, Vue, Solid, Svelte etc., which support re-rendering on the client.

  • Write the interactivity describing how to update the component in a &lt;script&gt; tag, for example with a custom element.

Which you choose is mostly up to personal preference and project requirements. If you have only a few instances of this kind of updating behaviour, I might choose to write a custom element to handle things. But if the project is heavily interactive, often a UI framework will help make that easier to build and maintain.

huangapple
  • 本文由 发表于 2023年6月29日 18:20:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580141.html
  • astro
  • astrojs

如何使用` :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定