mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-12 17:26:17 +00:00
fix inconsistent updates to react component state
Executions of setState() may be batched, and so updates to this.state and this.props can be asynchronous, so references to this.state and this.props should not be made in the new state, and instead the callback form of setState() should be used. These alerts were found using lgtm.com: https://lgtm.com/projects/g/BoostIO/Boostnote/alerts/?mode=tree&ruleFocus=1819283066 see: https://reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous
This commit is contained in:
@@ -55,10 +55,10 @@ class SnippetTab extends React.Component {
|
||||
this.handleRename()
|
||||
break
|
||||
case 27:
|
||||
this.setState({
|
||||
name: this.props.snippet.name,
|
||||
this.setState((prevState, props) => ({
|
||||
name: props.snippet.name,
|
||||
isRenaming: false
|
||||
})
|
||||
}))
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -368,11 +368,11 @@ class SnippetNoteDetail extends React.Component {
|
||||
name: mode
|
||||
})
|
||||
}
|
||||
this.setState({note: Object.assign(this.state.note, {snippets: snippets})})
|
||||
this.setState(state => ({note: Object.assign(state.note, {snippets: snippets})}))
|
||||
|
||||
this.setState({
|
||||
note: this.state.note
|
||||
}, () => {
|
||||
this.setState(state => ({
|
||||
note: state.note
|
||||
}), () => {
|
||||
this.save()
|
||||
})
|
||||
}
|
||||
@@ -381,11 +381,11 @@ class SnippetNoteDetail extends React.Component {
|
||||
return (e) => {
|
||||
const snippets = this.state.note.snippets.slice()
|
||||
snippets[index].mode = name
|
||||
this.setState({note: Object.assign(this.state.note, {snippets: snippets})})
|
||||
this.setState(state => ({note: Object.assign(state.note, {snippets: snippets})}))
|
||||
|
||||
this.setState({
|
||||
note: this.state.note
|
||||
}, () => {
|
||||
this.setState(state => ({
|
||||
note: state.note
|
||||
}), () => {
|
||||
this.save()
|
||||
})
|
||||
|
||||
@@ -399,10 +399,10 @@ class SnippetNoteDetail extends React.Component {
|
||||
return (e) => {
|
||||
const snippets = this.state.note.snippets.slice()
|
||||
snippets[index].content = this.refs['code-' + index].value
|
||||
this.setState({note: Object.assign(this.state.note, {snippets: snippets})})
|
||||
this.setState({
|
||||
note: this.state.note
|
||||
}, () => {
|
||||
this.setState(state => ({note: Object.assign(state.note, {snippets: snippets})}))
|
||||
this.setState(state => ({
|
||||
note: state.note
|
||||
}), () => {
|
||||
this.save()
|
||||
})
|
||||
}
|
||||
@@ -597,17 +597,17 @@ class SnippetNoteDetail extends React.Component {
|
||||
}
|
||||
|
||||
jumpNextTab () {
|
||||
this.setState({
|
||||
snippetIndex: (this.state.snippetIndex + 1) % this.state.note.snippets.length
|
||||
}, () => {
|
||||
this.setState(state => ({
|
||||
snippetIndex: (state.snippetIndex + 1) % state.note.snippets.length
|
||||
}), () => {
|
||||
this.focusEditor()
|
||||
})
|
||||
}
|
||||
|
||||
jumpPrevTab () {
|
||||
this.setState({
|
||||
snippetIndex: (this.state.snippetIndex - 1 + this.state.note.snippets.length) % this.state.note.snippets.length
|
||||
}, () => {
|
||||
this.setState(state => ({
|
||||
snippetIndex: (state.snippetIndex - 1 + state.note.snippets.length) % state.note.snippets.length
|
||||
}), () => {
|
||||
this.focusEditor()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user