generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move row can't trigger re-render
- Loading branch information
Showing
4 changed files
with
51 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
useEffect, | ||
useRef, | ||
} from 'preact/compat'; | ||
|
||
/** | ||
* Helps tracking the props changes made in a react functional component. | ||
* | ||
* Prints the name of the properties/states variables causing a render (or re-render). | ||
* For debugging purposes only. | ||
* | ||
* @usage You can simply track the props of the components like this: | ||
* useRenderingTrace('MyComponent', props); | ||
* | ||
* @usage You can also track additional state like this: | ||
* const [someState] = useState(null); | ||
* useRenderingTrace('MyComponent', { ...props, someState }); | ||
* | ||
* @param componentName Name of the component to display | ||
* @param propsAndStates | ||
* @param level | ||
* | ||
* @see https://stackoverflow.com/a/51082563/2391795 | ||
*/ | ||
export const useRenderingTrace = (componentName: string, propsAndStates: any, level: 'debug' | 'info' | 'log' = 'debug') => { | ||
const prev = useRef(propsAndStates); | ||
|
||
useEffect(() => { | ||
const changedProps: { [key: string]: { old: any, new: any } } = Object.entries(propsAndStates).reduce((property: any, [key, value]: [string, any]) => { | ||
if (prev.current[key] !== value) { | ||
property[key] = { | ||
old: prev.current[key], | ||
new: value, | ||
}; | ||
} | ||
return property; | ||
}, {}); | ||
|
||
if (Object.keys(changedProps).length > 0) { | ||
console[level](`[${componentName}] Changed props:`, changedProps); | ||
} | ||
|
||
prev.current = propsAndStates; | ||
}); | ||
}; |