react-docx is a React reconciler for DOCX.js.
Why
Largely inspired by R3F this library allows you write your DOCX documents in declarative style with reusable components. This is not a wrapper library and so DOCX classes are trasformed dynamicly into React components.
Limitations
In current early stage of library and because of inconsistent style of some methods in DOCX.js not all advanced DOCX features may work declaratively. Currently only single render run is supported, that means that using useState or passing another set of values to Context providers will have no effect. Alternatively you can call renderAsyncDocument again to rerender the document entirely.
Usage
Instal peer dependencies React and Docx! They are needed for JSX and DOCX elements.
import React from "react"; // that is needed for jsx to workimport renderAsyncDocument from "react-docx";import * as Docx from "docx"; // that is a peer dependency ; const Component = children text const ref = React; // use can use refs to access docx objects React; return <p => <t>text</t> children </p> ;;
API
For details on Docx refer to it's docs.
Core render function, returns promise that is resolved with rendered document object. You can use Docx tools to convert it to blob, as according to Docx examples.
;
Future Plans
- More shortcuts
- More fictive objects to remove boilerplate
- Add mutations to Document tree