luyx2412
3/22/2018 - 1:48 PM

High PureComponent React Native (compare include function)

High PureComponent React Native (compare include function)

// Author: @tomzaku from https://gist.github.com/tomzaku/ccda8d03b8a35c92025a84cd14d0369b
// import liraries

import React, { Component } from 'react';
import _ from 'lodash'

// create a component
export const isEqualObjectIncludeFunction = (first, second) => {
  return _.isEqualWith(first, second, (val1, val2) => {
    // Compare include function
    if (_.isFunction(val1) && _.isFunction(val2)) {
      return val1.toString() === val2.toString();
    }
  })
}

export class HighPureComponent extends Component {
  shouldComponentUpdate = (nextProps, nextState) => {
    return !isEqualObjectIncludeFunction(nextProps,this.props) || !isEqualObjectIncludeFunction(nextState,this.state);
  }
}