question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Characters are typed twice with both onBeforeChange and onChange

See original GitHub issue

Here’s a fiddle illustrating the behaviour: https://jsfiddle.net/4890mgej/4/

Code:

class MyEditor extends React.Component {

  constructor(props) {
    super(props)
    this.state = {
      state: Plain.deserialize('')
    }
  }

  onChange(state) {
    this.setState({ state })
  }
  
  onBeforeChange(state) {
  	return state.transform().setBlock('paragraph').apply()
  }

  render() {
    return (
      <Editor
        placeholder="Enter some text..."
        onChange={state => this.onChange(state)}
        onBeforeChange={state => this.onBeforeChange(state)}
        state={this.state.state}
      />
    )
  }

}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
soulwirecommented, May 11, 2017

I have a similar use case, where I need to setNodeByKey on a bunch of custom nodes to update their data (either onBeforeChange or onChange) and there are various reasons why a schema rule isn’t well suited. Unfortunately I’m also getting the duplicate text issue when using either callback.

As with @jatins’ example, being able to insert some transformations between when the next document state is computed from inputs and the render happens is occasionally very useful.

If onBeforeChange’s days are numbered, what about an alternative with a more explicit purpose of finalising state—a blunter instrument than a schema rule—which can be used with the understanding that it might cause suboptimal re-renders?

1reaction
soulwirecommented, May 11, 2017

If you’re not bothered about summoning the dark lord of hacks, something like this could do the job…

const Plugin = (options) => {
  let checksum
  return {
    schema: {
      rules: [
        {
          match: node => node.kind === 'document',
          validate: node => node.data.get('checksum') !== checksum ? node : null,
          normalize: (transform, node) => transform
            .setNodeByKey(node.key, { data: { checksum } })
            .call(transformAllTheThings)
        }
      ]
    },
    onBeforeChange() {
      checksum = Math.floor(Math.random() * 1e16).toString(32)
    }
  }
}

💻 :trollface:

It doesn’t play well with undo/redo though

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my onchange function called twice when using .focus()?
I can confirm myOnChange gets called twice on Chrome. But the context argument is the initial input field on both calls.
Read more >
HTML onchange Attribute - W3Schools
Definition and Usage. The onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to...
Read more >
typescript-cheatsheet - GitHub Pages
A set of TypeScript related notes used for quick reference. The cheatsheet contains references to types, classes, decorators, and many other TypeScript ...
Read more >
Calendar | Fomantic-UI Docs
A calendar allows users to select any date or time.
Read more >
CodeMirror 5 User Manual
Both the CodeMirror function and its fromTextArea method take as second ... the editor should re-indent the current line when a character is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found