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 12, 2024
1 parent 931f1e6 commit 9c7644b
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 @@ -83,7 +83,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 @@ -121,7 +121,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 9c7644b

Please sign in to comment.