C
C
chill842014-05-20 19:41:28
JavaScript
chill84, 2014-05-20 19:41:28

What is the best way to implement component state update in React.js for contenteditable?

The setState method calls the render method after itself , which is not critical if the behavior of the contenteditable element corresponds to the behavior of an ordinary form-input, but is critical if there are several dom elements in innerHTML at once, the cursor position is reset. The task is as follows:
1) inject any html code into the element
2) get the innerHTML value
3) apply the sanitize function to the innerHTML value
4) place the cleaned content in state.content
How to implement this?
Example:

sanitize = (content) ->
  cleanContent = ''
  # ...
  return cleanContent

ContentEditableView = React.createClass
  displayName: 'ContentEditableView'

  getInitialState: -> content: ''

  handleChange: (event) ->

    return

  render: ->
    {div} = React.DOM
    (div {  
        contentEditable: yes
        onInput:         @handleChange
        dangerouslySetInnerHTML: {
          __html: @state.content
        }
      })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
styv, 2014-07-13
@styv

Perhaps shouldComponentUpdate will help, if you return false, then the component will not be redrawn, but the state will be updated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question