Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two react-quill instances produces errkr #252

Closed
hmontes opened this issue Aug 4, 2017 · 5 comments
Closed

Two react-quill instances produces errkr #252

hmontes opened this issue Aug 4, 2017 · 5 comments

Comments

@hmontes
Copy link

hmontes commented Aug 4, 2017

https://codepen.io/anon/pen/ZJBWGO

If we call two times react-quill (Example. New comment and new replies like any social media). The second instance works bad. The template changes and the buttons don't work.

You can see the problem in the codepen.

Thanks

@alexkrolick
Copy link
Collaborator

alexkrolick commented Aug 4, 2017

Your example reuses the same ID for both toolbars

You can tweak it like this to take a prop for the toolbar ID instead: https://codepen.io/alexkrolick/pen/mMOPmv?editors=0010

Sorry for any confusion!

@hmontes
Copy link
Author

hmontes commented Aug 4, 2017

Do you have another method with a random id or continuous numbers? In the worst cases, the user can have multiple editor (I can avoid that. But just in case)

@alexkrolick
Copy link
Collaborator

See the last line where the editors are mounted - you can use any code there to generate a sequence of IDs.

@hmontes
Copy link
Author

hmontes commented Aug 4, 2017

Okey. I'll try. Thank you

@hmontes
Copy link
Author

hmontes commented Aug 4, 2017

I find another and better way. Require the library shortid

Outside the class

const CustomToolbar = ({shortId}) => (
  <div id={shortId}>
    <button className="ql-bold"></button>
    <button className="ql-italic"></button>
    <button className="ql-underline"></button>
    <button className="ql-insertStar" type="button">
      <CustomButton />
    </button>
    
  </div>
)

Class

class Editor extends React.Component {
  constructor(props) {
    super(props)
    this.state = { text: '' }
    this.handleChange = this.handleChange.bind(this)
    this.shortId = shortid.generate()
  }


  modules = (shortId) => {
    return {
      toolbar: {
        container: "#" + shortId,
        handlers: {
          "insertStar": insertStar,
        }
      }
    }
  }

  ... stuff like formats and handleChange

  render() {
    return (
      <div>
        <CustomToolbar shortId={this.shortId}/>
        <ReactQuill value={this.state.text}
          modules={this.modules(this.shortId)}
         ...
        />
      </div>
    )
  }

Works because the ID in unique. Enjoy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants