0

I have a react page builder, I am displaying user-created pages using dangerous HTML now I want when the user clicks a button preview to open this page on a new window.

Here is my component to display user pages

import React, { useState, useEffect} from "react";
import api from "../utils/api";

function MyPages() {
  const [MyPagesList, setMyPagesList] = useState();

  const getMyPages = async () => {
    try {
      const response = await api.get("/pages");
      setMyPagesList(response.data.data);
    } catch (error) {}
  };

  useEffect(() => {
    getMyPages();
  }, []);

  return (
        <div className="all-mypages">
          <div className="all-mypages__cards">
            {MyPagesList && MyPagesList.map(function (data, id) {
                return (
                  <div  className="mypages-card" key={id}>
                    <a className="mypages-card__link">
                      <div className="mypages-card__content"
                        dangerouslySetInnerHTML={{__html: data.attributes.html}}></div>
                    </a>
                    <button className="preview " onClick={()=>history.push(`/mypages/preview/${data.attributes.html}`)>Preview</button>
                  </div>
                );
              })}
          </div>
        </div>
  );
}

export default MyPages;

When user clicks preview button here is what I get

enter image description here

My solution is not working and I am out of ideas, what do I need to do to solve this issue?

The Dead Man
  • 6,258
  • 28
  • 111
  • 193

1 Answers1

0

The result is correct, because you are appending the html to /mypages/preview/. If you want the data.attributes.html ( which is the user html content ) to be displayed on a new page then:

  • Create another React component which looks for this html content from localStorage redux
  • When clicked on the preview button open a new page with url in react router pointing to this component
  • This component on load will get the localStorage html content and pass it to its dangerouslySetInnerHtml and display it.
Ameya Pai
  • 11
  • 1
  • 3
  • I had to use a global state management because if you want to open a react component in a new page, passing the html as a route parameter or storing it in localStorage is not a good idea. Also data is hardcoded because I didn't have time to setup a mock api server. Link to the solution https://github.com/paiameya/react-new-page-redirection @TheDeadMan – Ameya Pai Sep 06 '21 at 06:56