lalalic / docx4js

a javascript docx parser
355 stars 66 forks source link

Use of docx4js in ReactJS #27

Closed AkshayCloudAnalogy closed 2 years ago

AkshayCloudAnalogy commented 5 years ago

I am a newbie in React and i am not able to get this working in React. I have use axios to get the response of the file as "arraybuffer" and successfully able to get the data from docx4js.load but i am not able to understand how to render that on the screen.

Here is the code if that might help:

import React, { useEffect } from "react";
import axios from "axios";
import docx4js from "docx4js";

const Docx = ({ previewedRecord, token }) => {
  useEffect(() => {
    axios
      .get(previewedRecord.url, {
        responseType: "arraybuffer",
        headers: {
          Authorization: "Bearer " + token
        }
      })
      .then(res => {
        docx4js.load(res.data).then(docx => {
          console.log("[Docx.jsx] docx:", docx); // I am able to get the data here.
        })
          .catch(err => {
            console.error("[Docx.jsx] err:", err);
          });
      })
      .catch(err => {
        console.error("[Docx.jsx] err:", err);
      });
  }, []);

  return <div id="docx" />; // I want the Docx to be rendered here.
};

export default Docx;

Here is the screenshot of the docx returned:

image

lalalic commented 2 years ago

you can use docx.render((type,props,children)=><YourComponentForType {...props}/>)