Created
June 24, 2019 17:04
-
-
Save amhinson/21c4cef993de31bebb016cfda47e8bca to your computer and use it in GitHub Desktop.
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 { transform, isEqual, isObject } from "lodash"; | |
import Reactotron from "reactotron-react-native"; | |
export const stateChanges = (object: any, base: any): Object => { | |
return transform(object, (result, value, key) => { | |
if (!isEqual(value, base[key])) { | |
// @ts-ignore | |
result[key] = | |
isObject(value) && isObject(base[key]) | |
? stateChanges(value, base[key]) | |
: value; | |
} | |
}); | |
}; | |
export const consoleStateChanges = (prevState: any, newState: any) => { | |
const current = stateChanges(newState, prevState); | |
const prev = stateChanges(prevState, newState); | |
const keysUpdated = Object.keys(current); | |
Reactotron.display({ | |
name: "Context State Change", | |
value: { current, prev }, | |
preview: keysUpdated.join(", ") | |
}); | |
}; | |
// Usage | |
componentDidUpdate(prevProps: Props, prevState: State) { | |
if (__DEV__) { | |
consoleStateChanges(prevState, this.state); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment