Attempting to transform HTML code received from the server into an image, but encountering an error while using ReactJS

This app is designed to automate the process of creating social media posts. I have a template for the vertical "Cablgram" stored in the backend, and when I make a request, it returns the HTML code for that template. However, I encounter an error when trying to convert the image to display it using the domtoimage library. The error message reads: dom-to-image.js:191 Uncaught (in promise) TypeError: node.cloneNode is not a function.

import './Dashboard.css';
import { useEffect, useState } from 'react'
import { Grid, Image } from 'semantic-ui-react'
import axios from 'axios'
import domtoimage from 'dom-to-image'
import parse from 'html-react-parser'

function Dashboard(props) {
    let [posts, setPosts] = useState([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

    function retain(unsafe) {
        return unsafe
            .replaceAll("<", '<')
            .replaceAll(">", '>')
    }

    async function getPosts() {
        let { data } = await axios.post('http://localhost:3000/templates/get', { vertical_name: 'cablgram' })
        let articles = data.articles

        let article_htmls = articles.map(async (code) => {
            const html = retain(code)
            const actual_html = parse(html)
            const url = await domtoimage.toPng(wrapper)
            return url
        })

        setPosts(article_htmls)
    }

    useEffect(() => {
        getPosts()
    }, [])

    return (
        <div className="Dashboard page-container">
            <div className="title-container">
                <h1 className="title">Atlas</h1>
            </div>

            <div className="subtitle-container">
                <span className="subtitle">Curated news from contemporary culture.</span>
            </div>

            <div className="dashboard-grid-container">
            <Grid columns={3}>
                { 
                    posts.map((post, index) => (
                        <Grid.Column key={index}>
                            {post}
                        </Grid.Column>
                    ))
                }
            </Grid>
            </div>
        </div>
    )
  }
  
  export default Dashboard
  
  

I would appreciate some assistance in understanding what's causing this issue and how I can resolve it.

Answer №1

Give it a go:

// Copying Syntax:
let duplicate = element.cloneNode([deep])

// Example of Cloning a Node:
let div = document.getElementById("sampleDiv")
let new_div = div.cloneNode(true)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The world of coding comes alive with Quartz Composer JavaScript

Seeking assistance as I navigate through a challenging project. Any help would be greatly appreciated. Thanks in advance! I have a Quartz Composition where I aim to incorporate a Streetview from Google Maps and control the navigation, but I'm unsure ...

How can I show the total sum of input values in a React.js input box?

Is there a way to dynamically display the sum of values entered in front of my label that updates automatically? For example, you can refer to the image linked below for the desired output Output Image I have initialized the state but I'm struggling ...

It is impossible for me to invoke a method within a function

I am new to working with typescript and I have encountered an issue while trying to call the drawMarker() method from locateMe(). The problem seems to be arising because I am calling drawMarker from inside the .on('locationfound', function(e: any ...

Include additional content in the mui datepicker widget

Is there a way to include additional text in the mui datepicker? https://i.stack.imgur.com/wj84R.png import React, { useState } from "react"; import { DateTimePicker, KeyboardDateTimePicker } from "@material-ui/pickers"; function Inlin ...

Designing a nested function within a function encapsulated within a class

Suppose I have a class with a function inside: var myClass = class MyClass() { constructor() {} myFunction(myObj) { function innerFunction() { return JSON.stringify(myObj, null, 2); } return myObj; } } In this scenario, callin ...

Jquery allows for the toggling of multiple checkboxes

I have a group of check-boxes that I would like to toggle their content when checked. Currently, I am using jQuery to achieve this functionality, but I am searching for a way to optimize my code so that I do not need to write a separate function for each ...

Optimal method for conducting Jasmine tests on JavaScript user interfaces

After exploring the jasmine framework for the first time, I found it to be quite promising. However, I struggled to find a simple way to interact with the DOM. I wanted to be able to simulate user interactions such as filling out an input field, clicking ...

Unveiling the Magic: Enhancing Raphaeljs with Interactive Click Events on a Delicious Slice of the

I'm having trouble responding to a click event on each slice of a Raphael Pie Chart. I've tried implementing the code below, but it doesn't seem to be working. The code is just two lines, commented as "My Code", in the example from the offic ...

sending information from a PHP form to a JavaScript window

Currently, I am in the process of developing a game using javascript and jquery. In this game, when a player interacts with another character, it triggers the opening of text from an external file using the window.open('') function. At the start ...

Execute a PHP script to retrieve data from a database based on the content of a text element

I'm currently working on a web-based system and I'm wondering if it's possible to retrieve a Name based on the number entered in a text box. Here is what I have attempted so far, but I know it's not functioning properly. Is there an alt ...

What is the best way to pass a JSP variable to a ReactJS file?

I'm currently working on a project that involves using the spring framework and React.js. In one of my JSP files, I have a ${variable} that has been injected. However, I'm having trouble passing this variable to my React.js file. I tried writing ...

The parameters for Vue.js @change events are consistently returning as 0 whenever a file is uploaded by clicking on the picture

Check out my JSFiddle here:: https://jsfiddle.net/includephone/o9gpb1q8 I've encountered an issue involving the @change event on an input field. My goal is to create a carousel of images with 4 images per slide. Data Object Structure data: functio ...

Choose items in a particular order based on the class name of their category

How can I dynamically select and manipulate groups of elements? My intention is to select and modify the msgpvtstyleme elements, then select the msgpvtstyle elements separately and work with them as well. The ultimate goal is to apply classes to these grou ...

Employing CSS animations to elevate div elements

Currently, I am working on animating a table of divs and trying to achieve an effect where a new div enters and "bumps up" the existing ones. In my current setup, Message i3 is overlapping Message 2 instead of bumping it up. How can I make Messages 1 and 2 ...

Begin the NextJS project by redirecting the user to the Auth0 page without delay

I am new to coding and currently working on a project using Typescript/NextJS with Auth0 integration. The current setup navigates users to a page with a login button that redirects them to the Auth0 authentication page. However, this extra step is unneces ...

What is the best way to generate JSX from a callback function in ChartJS?

I am currently utilizing react-chartjs-2 within a React application and I am interested in incorporating JSX code into the Y axis. However, when attempting to do so, it only shows [Object object]. const chart = new Chart(ctx, { type: 'line', ...

Utilizing Vue.js and i18n to fetch external JSON files from a server and seamlessly integrating them as globally accessible data in all components

I'm currently exploring ways to fetch translation files from a server and make them accessible across all components of the project. For instance, I can request to obtain this JSON: { "DASHBOARD_SETTINGS": "Einstellungen", ...

Is there a method to incorporate a scroll event into the ng-multi-selectdropdown, a npm package?

Query: I need to incorporate a scroll event in the given html code that triggers when the div is scrolled. However, I am facing issues with implementing a scroll event that works when the elements are being scrolled. <ng-mult ...

JavaScript - Capture the Values of Input Fields Upon Enter Key Press

Here's the input I have: <input class="form-control" id="unique-ar-array" type="text" name="unique-ar-array" value="" placeholder="Enter a keyword and press return to add items to the array"> And this is my JavaScript code: var uniqueRowsArr ...

What is the best way to ensure that my grid item elements are all in a single line?

As a beginner in learning Material-ui, I encountered an error when using the Grid component. Here is the code snippet that caused the issue: My aim is to align the header buttons on the same line and have them appear with a scroll bar when zooming out. &l ...