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

Use callback state update for updates relying on previous (state, props) #1158

Merged
merged 1 commit into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ export default class Editable extends wp.element.Component {
}
} );

this.setState( {
formats: merge( {}, this.state.formats, formats ),
} );
this.setState( ( state ) => ( {
formats: merge( {}, state.formats, formats ),
} ) );

this.editor.setDirty( true );
}
Expand Down
6 changes: 3 additions & 3 deletions blocks/library/freeform/format-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class FormatList extends wp.element.Component {
}

toggleMenu() {
this.setState( {
open: ! this.state.open,
} );
this.setState( ( state ) => ( {
open: ! state.open,
} ) );
}

switchFormat( newValue ) {
Expand Down
44 changes: 22 additions & 22 deletions components/form-token-field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,22 @@ class FormTokenField extends Component {
}

handleUpArrowKey() {
this.setState( {
selectedSuggestionIndex: Math.max( ( this.state.selectedSuggestionIndex || 0 ) - 1, 0 ),
this.setState( ( state ) => ( {
selectedSuggestionIndex: Math.max( ( state.selectedSuggestionIndex || 0 ) - 1, 0 ),
selectedSuggestionScroll: true,
} );
} ) );

return true; // preventDefault
}

handleDownArrowKey() {
this.setState( {
this.setState( ( state, props ) => ( {
selectedSuggestionIndex: Math.min(
( this.state.selectedSuggestionIndex + 1 ) || 0,
this.getMatchingSuggestions().length - 1
( state.selectedSuggestionIndex + 1 ) || 0,
this.getMatchingSuggestions( state, props ).length - 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unfortunate a consequence from this sort of change. In general it'd be nice if we could at least create the function signature to include only those relevant to generating its result, not just accepting entire state and props of the instance. In this case it needs a fair few values (suggestions, incompleteTokenValue, value, max), so not sure if it's much of an improvement.

),
selectedSuggestionScroll: true,
} );
} ) );

return true; // preventDefault
}
Expand All @@ -244,21 +244,21 @@ class FormTokenField extends Component {
}

moveInputToIndex( index ) {
this.setState( {
inputOffsetFromEnd: this.props.value.length - Math.max( index, -1 ) - 1,
} );
this.setState( ( state, props ) => ( {
inputOffsetFromEnd: props.value.length - Math.max( index, -1 ) - 1,
} ) );
}

moveInputBeforePreviousToken() {
this.setState( {
inputOffsetFromEnd: Math.min( this.state.inputOffsetFromEnd + 1, this.props.value.length ),
} );
this.setState( ( state, props ) => ( {
inputOffsetFromEnd: Math.min( state.inputOffsetFromEnd + 1, props.value.length ),
} ) );
}

moveInputAfterNextToken() {
this.setState( {
inputOffsetFromEnd: Math.max( this.state.inputOffsetFromEnd - 1, 0 ),
} );
this.setState( ( state ) => ( {
inputOffsetFromEnd: Math.max( state.inputOffsetFromEnd - 1, 0 ),
} ) );
}

deleteTokenBeforeInput() {
Expand Down Expand Up @@ -341,20 +341,20 @@ class FormTokenField extends Component {
return token;
}

getMatchingSuggestions() {
let suggestions = this.props.suggestions;
let match = this.props.saveTransform( this.state.incompleteTokenValue );
getMatchingSuggestions( state = this.state, props = this.props ) {
let suggestions = props.suggestions;
let match = props.saveTransform( state.incompleteTokenValue );
const startsWithMatch = [];
const containsMatch = [];

if ( match.length === 0 ) {
suggestions = difference( suggestions, this.props.value );
suggestions = difference( suggestions, props.value );
} else {
match = match.toLocaleLowerCase();

each( suggestions, ( suggestion ) => {
const index = suggestion.toLocaleLowerCase().indexOf( match );
if ( this.props.value.indexOf( suggestion ) === -1 ) {
if ( props.value.indexOf( suggestion ) === -1 ) {
if ( index === 0 ) {
startsWithMatch.push( suggestion );
} else if ( index > 0 ) {
Expand All @@ -366,7 +366,7 @@ class FormTokenField extends Component {
suggestions = startsWithMatch.concat( containsMatch );
}

return take( suggestions, this.props.maxSuggestions );
return take( suggestions, props.maxSuggestions );
}

getSelectedSuggestion() {
Expand Down
6 changes: 3 additions & 3 deletions components/panel/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class PanelBody extends Component {

toggle( event ) {
event.preventDefault();
this.setState( {
opened: ! this.state.opened,
} );
this.setState( ( state ) => ( {
opened: ! state.opened,
} ) );
}

render() {
Expand Down
6 changes: 3 additions & 3 deletions editor/block-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class BlockSwitcher extends wp.element.Component {
}

toggleMenu() {
this.setState( {
open: ! this.state.open,
} );
this.setState( ( state ) => ( {
open: ! state.open,
} ) );
}

switchBlockType( name ) {
Expand Down
6 changes: 3 additions & 3 deletions editor/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class Inserter extends wp.element.Component {
}

toggle() {
this.setState( {
opened: ! this.state.opened,
} );
this.setState( ( state ) => ( {
opened: ! state.opened,
} ) );
}

close() {
Expand Down
2 changes: 1 addition & 1 deletion editor/sidebar/post-schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PostSchedule extends Component {

toggleDialog( event ) {
event.preventDefault();
this.setState( { opened: ! this.state.opened } );
this.setState( ( state ) => ( { opened: ! state.opened } ) );
}

handleClickOutside() {
Expand Down
2 changes: 1 addition & 1 deletion editor/sidebar/post-visibility/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PostVisibility extends Component {

toggleDialog( event ) {
event.preventDefault();
this.setState( { opened: ! this.state.opened } );
this.setState( ( state ) => ( { opened: ! state.opened } ) );
}

handleClickOutside() {
Expand Down