import React from "react"
import ReactDOM from "react-dom"
import "./styles.css"
function usePersistentValue(initialValue) {
return React.useState({
current: initialValue
})[0]
}
function Counter() {
const [count, setCount] = React.useState(0)
const id = usePersistentValue(null)
const clearInterval = () => window.clearInterval(id.current)
React.useEffect(() => {
id.current = window.setInterval(() => {
setCount(c => c + 1)
}, 1000)
return clearInterval
}, [])
return (
<div>
<h1>{count}</h1>
<button onClick={clearInterval}>Stop</button>
</div>
)
}
const rootElement = document.getElementById("root")
ReactDOM.render(<Counter />, rootElement)
here
function usePersistentValue(initialValue) {
return React.useState({
current: initialValue
})[0]
}
it seems a function call but followed by [0] makes no sense for me, especially what [0] doing here? is it valid javascript or a special syntax for react? my best guess here the [0] value gets assigned to current
property ,am i right? but im still confused how array value get assigned to the current
property, please help me explaining the syntax thank you