-
Notifications
You must be signed in to change notification settings - Fork 352
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
Removing createReactClass usage in 3 files. #1893
base: main
Are you sure you want to change the base?
Conversation
GeraldRequired Reviewers
Don't want to be involved in this pull request? Comment |
Size Change: -197 B (-0.02%) Total Size: 1.29 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of this is great. 🎉
Requesting changes for the comment about default props not being necessary for the SortableItem
changes.
} | ||
|
||
render() { | ||
const opacity = this.dragHover ? {opacity: 0.3} : {}; | ||
const Component = this.props.component; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this and the next few lines can be accomplished in ES6 more idiomatically.
const Component = this.props.component; | |
const {component: Component, shouldDragHighlight, ...forwardProps } = this.props.component; |
You won't need to delete
props off of the object then.
style: Props["style"]; | ||
}; | ||
|
||
export class DragTarget extends React.Component<Props> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an example of a component that is so simple it could be converted all the way to a functional component. It consists of rendering a component and some callback handlers. These are all easily modelled in a functional component. :)
Not required for this PR, but if you're feeling adventurous, go for it.
@@ -1,69 +1,91 @@ | |||
import {css, StyleSheet} from "aphrodite"; | |||
import createReactClass from "create-react-class"; | |||
import PropTypes from "prop-types"; | |||
import * as React from "react"; | |||
import ReactDOM from "react-dom"; | |||
|
|||
const PT = PropTypes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can delete this and the prop-types
import now, I think.
getInitialState() { | ||
return {dragHover: false}; | ||
}, | ||
handleDrop: function (e) { | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When initial state doesn't depend on anything, I typically avoid the function getInitialState()
and just define state like this:
state: State = {dragHover: false};
style: Props["style"]; | ||
}; | ||
|
||
export class DragTarget extends React.Component<Props> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use state
in this component, so it'd be a good idea to provide a State
generic argument.
export class DragTarget extends React.Component<Props> { | |
export class DragTarget extends React.Component<Props, State> { |
|
||
type ItemProps = { | ||
area: any; | ||
component: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we type this as React.ReactNode
? As far as I can see there are no constraints on what this is except that it is something React can render.
handleDragStart: function (e) { | ||
export class SortableItem extends React.Component<ItemProps> { | ||
static defaultProps: DefaultItemProps = { | ||
area: "", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be better to not provide all these defaults. The Sortable
that renders these items looks like it provides all of these props. By making defaults for these props, we make the prop optional, and force this component to have a good default. In the case of area
, the default is of the wrong type as it should be an object that provides three callback functions: onDragEnter
, onDragStart
, and onDrop
.
import * as React from "react"; | ||
import _ from "underscore"; | ||
|
||
import EditorPage from "./editor-page"; | ||
|
||
type Props = { | ||
componentClass: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can type this as React.ComponentType
.
} | ||
|
||
render() { | ||
const opacity = this.dragHover ? {opacity: 0.3} : {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be this.state.dragHover
. Then you don't need the dragHover: any;
instance variable above (which is never set).
const opacity = this.dragHover ? {opacity: 0.3} : {}; | |
const opacity = this.state.dragHover ? {opacity: 0.3} : {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure DragTarget
isn't dead code? I only see one usage, in perseus-editor/src/editor.tsx
. That usage doesn't pass the component
prop, so it seems like that DragTarget
would throw if it were ever rendered.
Summary:
Removing createReactClass usage in the following three files:
Issue: LEMS-365
Test plan:
Run "yarn test" and confirm no errors or regressions are introduced.
Run "yarn start" and look and view if there are any issues with any Perseus components.