This file contains 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'; | |
const withSearch = (WrappedComponent) => { | |
class WithSearch extends React.Component {/*...*/} | |
hoistNonReactStatic(EithSearch, WrappedComponent); | |
return WithSearch; | |
} | |
// Define a static method | |
ProductsList.staticMethod = function() {/*...*/} |
This file contains 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() { | |
// A new version of ProductsListWithSearch is created on every render | |
const ProductsListWithSearch = withSearch(ProductsList); | |
// That causes the entire subtree to unmount/remount each time! | |
return <ProductsListWithSearch />; | |
} |
This file contains 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 withSearch = WrappedComponent => { | |
class WithSearch extends React.Component { | |
state = { | |
searchTerm: "" | |
}; | |
handleSearch = event => { | |
this.setState({ searchTerm: event.target.value }); | |
}; | |
render() { |
This file contains 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 ProductsList = (props) => { | |
let { data: products } = props; | |
return ( | |
<div> | |
<div> | |
<div> | |
<h2>Products</h2> | |
</div> | |
</div> | |
<div> |
This file contains 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 products from './products.json' | |
class ProductsListWithSearch extends React.PureComponent { | |
state = { | |
searchTerm: '' | |
} | |
handleSearch = event => { | |
this.setState({ searchTerm: event.target.value }) | |
} | |
render() { |
This file contains 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 ProductCard = props => { | |
return ( | |
<div> | |
<hr /> | |
<p> | |
<b>Title:</b> {props.title} | |
</p> | |
<p> | |
<b>Style:</b> {props.style} | |
</p> |
This file contains 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 ProductCard = props => { | |
return ( | |
<div className="product"> | |
<p> | |
<b>Title:</b> {props.title} | |
</p> | |
<p> | |
<b>Style:</b> {props.style} | |
</p> | |
<p> |
This file contains 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 propsProxyHOC = (WraapperComponent) => { | |
return class extends React.Component { | |
state = { | |
count: 0 | |
} | |
render() { | |
// Pass thru the props that the HOC receives | |
return <WraapperComponent {...this.props} {...this.state} /> | |
} | |
} |
This file contains 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
// Example #1 | |
const twice = (f, v) => f(f(v)) | |
const add3 = v => v + 3 | |
twice(add3, 7) // 13 | |
// Example #2 | |
const filter = (predicate, xs) => xs.filter(predicate) | |
const is = type => x => Object(x) instanceof type |
This file contains 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 React from 'react' | |
const higherOrderComponent = WrappedComponent => { | |
class HOC extends React.Component { | |
render() { | |
return <WrappedComponent /> | |
} | |
} | |
return HOC | |
} |