Skip to content

Instantly share code, notes, and snippets.

@amhinson
Created June 24, 2019 17:04
Show Gist options
  • Save amhinson/21c4cef993de31bebb016cfda47e8bca to your computer and use it in GitHub Desktop.
Save amhinson/21c4cef993de31bebb016cfda47e8bca to your computer and use it in GitHub Desktop.
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