#what is the the best way to access a document Element
6 messages · Page 1 of 1 (latest)
You can use scripts https://docs.astro.build/en/core-concepts/astro-components/#client-side-scripts or you can use a framework like SolidJS, Svelte, Vue, React, and more https://docs.astro.build/en/guides/integrations-guide/#article
for example something like this ?
import {useEffect} from "preact/hooks";
import { h } from "preact";
export function getStaticPaths(): string {
let text: string = "";
useEffect(() => {
//@ts-ignore
text = document.getElementById("test").innerText;
});
return text;
}
isn't that good enough ? Would you expect an answer that involves a particular third party framework, or is your question intended for pure Astro ?
There's also the querySelector() and querySelectorAll() functions
https://github.com/MicroWebStacks/astro-big-doc/blob/b3600c5dd0a6bf4e75e1d1a58ae19a9826c82d05/src/components/panzoom.astro#L135
Also an important point in Astro, if you use client side js, you might need a way to sync usage of multiple instances of the same component. What I do is I create a unique id that I use to select in the query, not sure if Astro should handle/obfuscate such thing in the future or stay as pure and as lightweight as its original intentions
const container = document.querySelector(`.container[data-uid="${uid}"]`)
Sorry maybe I should have detailed my Problem more. I want to add a jwt query to my routes. the jwt gets written into an html div, when my server renders the page.
My first suggestion to solve my problem was to use the preact code snipped but this results just in an error.
Thats why asked the more generell to remove the prect code with a better solution
hmm, your explanation is even more intriguing. If I would think about it at meta level, I would say, try to do it with vanilla js and make it work even if the code is ugly, at least you understand it, and you'd have a code basis to share with a link if it does not work. Once there, you can think of optimization on second step by adopting a framework or refactoring your routines. Anything that is not clear and cannot be debugged will either not work or suprise you with bugs 😉
I'm not a front end experts but if you have a link to a non working project, I'd give it a try, I'm also learning jwt at the moment and would like to add github authentication, which porbably does not need jwt, but jwt might be handing to protect some apis some times.