This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>React</title> | |
</head> | |
<body> | |
<div id="root"></div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function logProps(Component) { | |
class LogProps extends React.Component { | |
componentDidUpdate(prevProps) { | |
console.log('old props:', prevProps); | |
console.log('new props:', this.props); | |
} | |
render() { | |
const {forwardedRef, ...rest} = this.props; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use this method instead... | |
MyComponent.someFunction = someFunction; | |
export default MyComponent; | |
// ...Export the method separately... | |
export { someFunction }; | |
// .. and in the components to be used, import them | |
import MyComponent, { someFunction } from "./MyComponent.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hoistNonReactStatic from "hoist-non-react-statics"; | |
function enhance(WrappedComponent) { | |
class Enhance extends React.Component {/*...*/} | |
hoistNonReactStatic(Enhance, WrappedComponent); | |
return Enhance; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function enhance(WrappedComponent) { | |
class Enhance extends React.Component {/*...*/} | |
// Must know exactly which methods should be copied :( | |
Enhance.staticMethod = WrappedComponent.staticMethod; | |
return Enhance; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define static function | |
WrappedComponent.staticMethod = function() {/*...*/} | |
// Use HOC now | |
const EnhancedComponent = enhance(WrappedComponent); | |
// Enhanced component does not have staticMethod | |
typeof EnhancedComponent.staticMethod === "undefined" // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render() { | |
// Each call to the render function creates a new EnhancedComponent | |
// EnhancedComponent1 !== EnhancedComponent2 | |
const EnhancedComponent = enhance(MyComponent); | |
// This will cause the subtree to be unloaded and remounted every time it is rendered! | |
return <EnhancedComponent />; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render() { | |
// Each call to the render function creates a new EnhancedComponent | |
// EnhancedComponent1 !== EnhancedComponent2 | |
const EnhancedComponent = enhance(MyComponent); | |
// This will cause the subtree to be unloaded and remounted every time it is rendered! | |
return <EnhancedComponent />; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EnhancedComponent = withRouter(connect(commentSelector)(WrappedComponent)) | |
// You can write combined tool functions | |
// compose(f, g, h) Equivalent to (...args) => f(g(h(...args))) | |
const enhance = compose( | |
// These are single-parameter HOCs | |
withRouter, | |
connect(commentSelector) | |
) | |
const EnhancedComponent = enhance(WrappedComponent) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// React Redux's `connect` Function | |
const ConnectedComment = connect(commentSelector, commentActions)(CommentList); | |
// connect is a function, and its return value is another function. | |
const enhance = connect(commentListSelector, commentListActions); | |
// The return value is HOC, which will return components that have been connected to the Redux store | |
const ConnectedComment = enhance(CommentList); |