Skip to content

Commit

Permalink
Allow setState to take a partial state
Browse files Browse the repository at this point in the history
React will shallow merge the provided object into the existing state¹
which works for our usage. The type definition of setState does not
allow partial state by default. Use 'never' to configure it to expect no
fields in particular².

¹ <https://react.dev/reference/react/Component#setstate-parameters>
² <https://stackoverflow.com/a/55824499>
  • Loading branch information
victorlin committed Nov 7, 2024
1 parent 302439f commit ad1f2ce
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/components/tree/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class TreeComponent extends React.Component<TreeComponentProps, TreeCompo
this.setUpAndRenderTreeToo(this.props, newState); /* modifies newState in place */
}
newState.geneSortFn = sortByGeneOrder(this.props.genomeMap);
this.setState(newState); /* this will trigger an unnecessary CDU :( */
this.setState<never>(newState); /* this will trigger an unnecessary CDU :( */
}
}

Expand Down Expand Up @@ -175,7 +175,7 @@ export class TreeComponent extends React.Component<TreeComponentProps, TreeCompo
if (this.tangleRef && (leftTreeUpdated || rightTreeUpdated)) {
this.tangleRef.drawLines();
}
if (Object.keys(newState).length) this.setState(newState);
if (Object.keys(newState).length) this.setState<never>(newState);
}

override componentWillUnmount() {
Expand Down

0 comments on commit ad1f2ce

Please sign in to comment.