0

This is something similar to what i have excluding any others divs:

import React from "react";

export default function Post({post}) {

<div className="postDesc">
    {post.sanitizedHtml} //This contains p tag inside
</div>

    )
}

This post prop is coming from an api and post.sanitizedHtml basically contains raw html that has tags such as p ,h2 etc.

What i want is to access the p tag that is inside post.sanitizedHtml so that i can use the javascript property .textContent on it and extract the information.

But i think there is no method in react like document.querySelector or document.getElementBy:Id/Classname

Also i think i can't use refs because the data is coming in dynamically and i cant write ref={something} as a prop

So what should i do to access the p tag that is there inside the post.sanitizedHtml ?

Your fast response is appreciated.

4
  • Have you tried it with dangerouslySetInnerHTML ? Commented Oct 4, 2021 at 6:07
  • @HarshitRastogi I think that is just going to render the whole html which i have done on the other pages of the website but I just want the p tags how can i access it? Commented Oct 4, 2021 at 6:09
  • @Saswat you can set a ref on the outer div, and access its child nodes. This obviously means you'd have to know beforehand what you want to access inside the dynamic html. Commented Oct 4, 2021 at 6:26
  • Thanks @AbhishekSharma for your wonderful suggestion. I was thinking of something like that. If you can modify my code and post this as an answer to how can i exactly access the child nodes on the div it would be great Commented Oct 4, 2021 at 6:41

2 Answers 2

2

By setting a ref on the outer div, you can access its child nodes and change their properties as you like.

Read more about Child Nodes

import React, { useRef, useEffect } from 'react';

export const Post = ({post}) => {
  
  const containerRef = useRef(null);
  
  //Let's Change something inside the post
  useEffect(() => {
    if(!post.sanitizedHtml || !containerRef.current) return;
    
    const container = containerRef.current;
    const childNodes = container.childNodes; //This will be a list of child nodes
    /* let's say 0th element of childNodes is a p tag */
    childNodes[0].innerText = "Hey I changed Something";
    
    
  }, [post, containerRef])
  
  return(
    <div className="postDesc" ref={containerRef}>
      {post.sanitizedHtml}
    </div>
  )
}
Sign up to request clarification or add additional context in comments.

Comments

1

In your situation, are you able to create an element with javascript and assign the sanitizedHtml to it, then just reference it from the newly created element?

const myElement = document.createElement("DIV"); 
myElement.innerHTML = post.santizedHtml;

then lookup from myElement

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.