Characters are typed twice with both onBeforeChange and onChange
See original GitHub issueHere’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:
- Created 6 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
Top 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 >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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have a similar use case, where I need to
setNodeByKey
on a bunch of custom nodes to update their data (eitheronBeforeChange
oronChange
) 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?
If you’re not bothered about summoning the dark lord of hacks, something like this could do the job…
💻 :trollface:
It doesn’t play well with undo/redo though