Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Habanero = props => <div>{props.children}</div>;
  2.  
  3.     const Testing = Component => {
  4.         return class extends React.Component {
  5.             constructor(props) {
  6.                 super(props);
  7.  
  8.                 this.state = {
  9.                     test: null
  10.                 }
  11.             }
  12.  
  13.             setTimer = () => {
  14.                 if (this._timer != null) {
  15.                     clearTimeout(this._timer)
  16.                 }
  17.  
  18.                 this._timer = setTimeout(() => {
  19.                     this.setState({test: null});
  20.                     this._timer = null;
  21.                 }, 1600);
  22.             };
  23.  
  24.             componentWillUnmount() {
  25.                 clearTimeout(this._timer);
  26.             }
  27.  
  28.             /*shouldComponentUpdate(nextProps) {
  29.                 if (nextProps.odds !== this.props.odds) {
  30.                     console.log(nextProps, this.props);
  31.  
  32.                     const change = nextProps.odds > this.props.odds ? "up" : "down";
  33.  
  34.                     this.setTimer();
  35.  
  36.                     this.setState({test: change});
  37.                 }
  38.  
  39.                 return true;
  40.             }*/
  41.  
  42.             //
  43.             // static getDerivedStateFromProps(props, state) {
  44.             //     console.log(props);
  45.             //}
  46.  
  47.             getSnapshotBeforeUpdate(prevProps) {
  48.                 if(prevProps.odds === this.props.odds) return null;
  49.  
  50.                 return prevProps.odds < this.props.odds ? "up" : "down";
  51.             }
  52.  
  53.             componentDidUpdate(prevProps, prevState, snapshot) {
  54.                 if(snapshot) {
  55.                     console.log(snapshot);
  56.  
  57.                     this.setTimer();
  58.  
  59.                     this.setState({test: snapshot});
  60.                 }
  61.  
  62.                 console.log("updated");
  63.             }
  64.  
  65.             render() {
  66.                 const {odds, ...rest} = this.props;
  67.                 const child = this.state.test ? <div className={`odds odds--${this.state.test}`}></div> : undefined;
  68.  
  69.                 return <Component {...rest}>
  70.                     {child}
  71.                 </Component>
  72.             }
  73.         }
  74.     };
  75.  
  76.     const Asd = Testing(Habanero);
  77.  
  78.     return (
  79.         <Wrapper
  80.             render={({increment, decrement, count}) => (
  81.                 <div>
  82.                     <div>
  83.                         <h3>Render Props Counter</h3>
  84.                         <Asd odds={count}/>
  85.                     </div>
  86.                     <div>
  87.                         <div>{count}</div>
  88.                         <button onClick={() => increment()}>Increment</button>
  89.                         <button onClick={() => decrement()}>Decrement</button>
  90.                     </div>
  91.                 </div>
  92.             )}
  93.         >
  94.             {alabala => (
  95.                 <div>Zakkie, u r the best{alabala}</div>
  96.             )}
  97.         </Wrapper>)
  98. };
  99.  
  100. class Wrapper extends React.Component {
  101.     state = {
  102.         count: 0
  103.     };
  104.  
  105.     // Increase count
  106.     increment = () => {
  107.         const { count } = this.state;
  108.         return this.setState({ count: count + 1 });
  109.     };
  110.  
  111.     // Decrease count
  112.     decrement = () => {
  113.         const { count } = this.state;
  114.         return this.setState({ count: count - 1 });
  115.     };
  116.  
  117.     render() {
  118.         const { count } = this.state;
  119.  
  120.         return (
  121.             <div>
  122.                 {this.props.render({
  123.                     increment: this.increment,
  124.                     decrement: this.decrement,
  125.                     count: count
  126.                 })}
  127.                 {this.props.children("!!!")}
  128.             </div>
  129.         );
  130.     }
  131. };
  132.  
  133. export default Wrapper;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement