#How to add custom component in script
9 messages · Page 1 of 1 (latest)
Hi @stable gyro. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Astro, you can import your custom component and use it within your Astro component. You can pass props to your custom component, which in your case would be the transaction data. Here's a general example of how you can do this:
---
import TrxRecord from '../components/TrxRecord.astro';
const trxData = await fetchTransactionData(); // Your function to fetch transaction data
---
<!-- Use the TrxRecord component for each transaction data -->
{trxData.map((data) => (
<TrxRecord data={data} />
))}
In the script section (between ---), you import your custom component and fetch the transaction data. Then, in the HTML section, you use the TrxRecord component for each transaction data.
Please replace fetchTransactionData with your actual function to fetch transaction data and adjust the path to the TrxRecord component as necessary.
Remember, the code in the script section runs on the server and is not available in the browser. If you need to pass variables from the server to the client, you can use data-* attributes to store the value of variables in your HTML output. Scripts can then read these attributes using an element’s dataset property once your HTML loads in the browser. Learn more here (https://docs.astro.build/en/guides/client-side-scripts/#pass-frontmatter-variables-to-scripts).
Also, note that Astro uses a code fence (---) to identify the component script in your Astro component. You can use the component script to write any JavaScript code that you need to render your template. This can include importing other Astro components, importing data, fetching content from an API or database, creating variables that you will reference in your template, etc. Learn more here (https://docs.astro.build/en/basics/astro-components/#the-component-script).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@stable gyro kapa.ai is still learning and improving, please let us know how it did by reacting below
I can only get trxData in script, I mean I can get them in client side @fluid cypress
@fluid cypress I can only get trxData in script, I mean I can get them in client side
Let me check the knowledge base for you. One moment! :books:
If you need to fetch the transaction data on the client-side, you can use JavaScript in a <script> tag within your Astro component. Here's a general example of how you can do this:
---
import TrxRecord from '../components/TrxRecord.astro';
---
<div id="trx-history"></div>
<script>
fetch('/api/transaction-data') // Replace with your API endpoint
.then(response => response.json())
.then(trxData => {
const trxHistoryContainer = document.getElementById('trx-history');
trxData.forEach(data => {
const trxRecord = document.createElement('trx-record');
trxRecord.data = data;
trxHistoryContainer.appendChild(trxRecord);
});
});
</script>
In this example, you fetch the transaction data from an API endpoint on the client-side. Then, for each transaction data, you create a TrxRecord custom element and append it to the trx-history container.
Please replace /api/transaction-data with your actual API endpoint to fetch the transaction data and adjust the path to the TrxRecord component as necessary.
Remember, in Astro, <script> tags in Astro components are now built, bundled, and optimized by default. If you want to revert a <script> tag to previous default behavior (unbuilt, unbundled, untouched by Astro), you can use the <script is:inline> directive. Learn more here (https://docs.astro.build/en/guides/upgrade-to/v1/#new-default-script-behavior).