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

Gives a warning when visiting editor.value during onChange #2165

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/slate-react/src/components/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Editor extends React.Component {
tmp = {
change: null,
isChanging: false,
isResolvingValue: false,
operationsSize: null,
plugins: null,
resolves: 0,
Expand Down Expand Up @@ -208,6 +209,14 @@ class Editor extends React.Component {
return this.tmp.value
}

if (this.tmp.isResolvingValue) {
warning(
false,
`editor.value is initializing during onChange. Please call editor.props.value instead.`
)
return this.props.value
}

const value = this.resolveValue(this.plugins, this.props.value)
return value
}
Expand Down Expand Up @@ -390,13 +399,20 @@ class Editor extends React.Component {
resolveValue = memoizeOne((plugins, value) => {
debug('resolveValue', { plugins, value })
let change = value.change()
change = this.resolveChange(plugins, change, change.operations.size)
this.tmp.isResolvingValue = true

try {
change = this.resolveChange(plugins, change, change.operations.size)
} catch (error) {
throw error
} finally {
this.tmp.isResolvingValue = false
}

// Store the change and it's operations count so that it can be flushed once
// the component next updates.
this.tmp.change = change
this.tmp.operationsSize = change.operations.size

return change.value
})
}
Expand Down