Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display the content of React Quill without the html markup?

I managed to get my Quill working, but now I wanted to make a nice splitscreen as we have on this forum but one thing I haven't been able to figure out is how to convert the input of Quill to nice text on the preview side.

I'm able to display the text but it still has all the html tags which of course I don't want.

So this is my Quill setup so far:

export default class AddSpark extends Component {
  constructor(props) {
    super(props);

    this.onChange = this.onChange.bind(this);

    this.state ={
      content: '',
    };
  }

  onChange(html) {
    this.setState ({ content: html });
      console.log(html)
    }

  render() {
    return (
      <div>
      <Col xs={12} md={6}>
        <form ref={(input) => this.sparkForm = input} onSubmit={(e) => this.createSpark(e)}>

            <ControlLabel>Select your city</ControlLabel>
            <select id="formControlsCity" placeholder="Choose your city" onChange={this.onChange} className="form-control" onClick={ moreOptions } ref={(input) => this.city = input}>
              <option value="select">Choose your city</option>
              <option value="Beijing">Beijing</option>
              <option value="Shanghai">Shanghai</option>
              <option value="Chengdu & Chongqing">Chengdu & Chongqing</option>
            </select>
       
            <ControlLabel>Select your person</ControlLabel>
            <select id="formControlsPerson" placeholder="Choose your person" className="form-control" ref={(input) => this.person = input}>
              <option value="select">First select your city</option>
            </select>
    

            <ControlLabel>Select your location</ControlLabel>
            <select id="formControlsLocation" placeholder="Choose your location" className="form-control" ref={(input) => this.location = input}>
              <option value="select">First select your city</option>
            </select>

            <ControlLabel>Title</ControlLabel>
            <input type="text" label="Title" placeholder="Enter your title" className="form-control" ref={(input) => this.title = input}/>
          

            <ControlLabel>Content</ControlLabel>
              <div className='_quill'>
                <ReactQuill
                  ref='editor'
                  onChange={this.onChange}
                />
              </div>
              <br />

          <Button type="submit">Submit</Button>
        </form>
      </Col>
      <Col xs={12} md={6}>
      <h3>Preview</h3>
        {this.state.content}
      </Col>

      </div>
  )}
}

At the moment I get this: enter image description here

Any help is highly appreciated!

like image 416
Deelux Avatar asked Dec 03 '16 20:12

Deelux


2 Answers

The simplest way is to use the same react-quill component with editing disabled. This method doesn't need you to install any extra package. You can do it by passing a prop readOnly whose value is set to true (By default it is false)

Here is the code for the component which you can use to preview side:

<ReactQuill
   value={this.state.content}
   readOnly={true}
   theme={"bubble"}
/>

This is how it will look like: sample image

Note: ReactQuill comes with two inbuilt themes (bubble and snow).

Here is how it looks in the snow theme. It has a toolbar at the top: bubble

And here is how it looks in bubble theme. snow theme

You should use the "bubble" theme by passing the string "bubble" in the theme prop to display your editor content. This is because it does not have a toolbar at the top like the "snow" theme.

In order to use the bubble theme, you'll also have to import a CSS stylesheet for this specific theme as follow:*

import 'react-quill/dist/quill.bubble.css'
like image 63
iShubhamPrakash Avatar answered Sep 28 '22 02:09

iShubhamPrakash


After doing some research I was able to find the answer:

To display the content of Quill in the preview section without the html tags I used this code:

      <div dangerouslySetInnerHTML={{__html: this.state.content}}></div>
like image 33
Deelux Avatar answered Sep 28 '22 02:09

Deelux