Last active
September 20, 2023 13:41
-
-
Save Noitidart/061f1202ab12b3cafd48bf6c1383402a to your computer and use it in GitHub Desktop.
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
function useRenderCounter(label) { | |
const ref = React.useRef(); | |
React.useEffect(() => { | |
ref.current.textContent = [ | |
parseInt(ref.current.textContent || "0", 10) + 1, | |
label | |
] | |
.filter(Boolean) | |
.join(" - "); | |
}); | |
return ( | |
<span | |
style={{ | |
backgroundColor: "#ccc", | |
borderRadius: 4, | |
padding: "2px 4px", | |
fontSize: "0.8rem", | |
margin: "0 6px", | |
display: "inline-block" | |
}} | |
ref={ref} | |
/> | |
); | |
} |
HTML version without useEffect:
function RenderCounter(props: { label?: string } ) {
const savedCount = useRef(0);
return (
<span
style={{
backgroundColor: "#ccc",
borderRadius: 4,
padding: "2px 4px",
fontSize: "0.8rem",
margin: "0 6px",
display: "inline-block"
}}
>
{Boolean(label) ? `${label}: ` : ''}{++savedCount.current}
</span>
);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
React Native