0

I would like to know how to display dynamic script/html content in editor using react (react-froala-wysiwyg). It displays but the dynamic data not working proper shows as ${orderdate!''}

import React from "react";
import 'froala-editor/css/froala_style.min.css';
import 'froala-editor/css/froala_editor.pkgd.min.css';
import FroalaEditor from 'react-froala-wysiwyg';

class AddEmail extends React.PureComponent {
  constructor(props) {
    super(props);


handleChange =()=>{
 this.setState({ emailbody: value });
}

render() {
 return (
  <React.Fragment>
           <div className="form-group col-lg-12 col-sm-12">
            <FroalaEditor
              model={this.state.emailbody}
              onModelChange={this.handleChange}
            />
          </div>    
  </React.Fragment>
);
}

 }
}


// Dynamic data from backend data

emailbody: {
<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <p>Order Confirm</p>
  ${orderdate!''} 
</body>
</html>
}
miyavv
  • 673
  • 8
  • 18

1 Answers1

0

As your data for email body comes as a string and not JSX, you will need to replace in the string itself.

const reactStringReplace = require('react-string-replace');
const backEndResponseString = "content here";
const emailBody = reactStringReplace(backEndResponseString, '${orderdate!''}', (match, i) => (
  <span>${orderdate}</span>
));
this.setState({
emailBody : emailBody
})

incase you had JSX content then the same would have worked as follows:

this.state = {
      emailbody: `
        <!DOCTYPE html>
        <html lang="en">
        <head>
        <title></title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        </head>
        <body>
        <p>Order Confirm</p>
          ${orderdate || ""} 
        </body>
        </html>`
    };

but since your backend response is string, it will require the value to be replaced.

shubham
  • 314
  • 1
  • 3