Untitled

aaron's profile image
By aaron

I've been learning Rescript lately with React. One problem I've struggled with is trying to figure out how to use Rescript React to get the content from an HTML meta tag. I am unable to find documentation that explains how to do it. Through trial and error, I've found a way to do it.

Suppose you have a blog and on the blog post page you have a meta tag that stores the post ID for the post to make it accessible in javascript. Your meta tag looks like this:

<meta name="post_id" content="12345" />

To get the blog post ID from the meta tag using Rescript React, do this:

// First, get the meta tag
switch(ReactDOM.querySelector("meta[name='post_id']"]){
| Some(metaTag) => {
// Get the content attribute by converting the element to an object
let tag = ReactDOM.domElementToObj(metaTag)
let postId = tag["content"]
Js.log(postId)
}
| None => Js.log("Couldn't find the meta tag")
}

This is what I've discovered through experimentation, but it works. If there is a more "proper" way to do it I'm interested to hear it.

Support the author

This author accepts donations via the services listed below. Your donation will help them continue to create great content!

* Lernabit doesn't take any of the money from your donation, but the donation services or payment processors might take a fee. These trademarks are the property of their respective owners.

Replies

Login or signup to leave a reply.

Signup Login
No more replies to show here