-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Basic] useRef in useEffect Object is possibly undefined #187
Comments
Typescript seems to like this one import React, { useEffect, useRef } from 'react'
export default function() {
const myRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const node = myRef.current
node?.classList.add('something')
// ^ note the new ? similar to `if (node)`
})
return (
<div ref={myRef}></div>
)
} |
That second one is the proper usage.
…On Sun, Feb 16, 2020 at 5:47 AM tcme ***@***.***> wrote:
Typescript seems to like this one
import React, { useEffect, useRef } from 'react'
export default function() {
const myRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const node = myRef.current
node?.classList.add('something')
// ^ note the new ? similar to `if (node)`
})
return (
<div ref={myRef}></div>
)
}
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#187>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AC5MIALMZ2NQLG44FXRH62TRDEKVBANCNFSM4KWBX52A>
.
|
ok thanks I'll go with that, but technically useRef does not directly return a HTMLDivElement, is that correct? It returns some object that has a current property that is the HTMLDivElement… Am I missing something? |
You don't always have to declare the type your ref will be if you know the type won't change. For example, if you do something like const myRef = useRef(false);
if (!myRef.current) {
myRef.current = true
} typescript will infer it as a boolean: |
@hellatan there are overloads for // @type = { readonly current: HTMLDivElement | null }
const refObjectToPassIntoRefProp = useRef<HTMLDivElement>(null)
// @type = { current: HTMLDivElement | null }
const refObjectYouMaintain = useRef<HTMLDivElement | null>(null)
// @type = { current: HTMLDivElement | undefined }
const refObjectYouMaintainToo = useRef<HTMLDivElement>() With the first, the |
@ferdaber so do you mean when you don't pass in |
Correct. |
Good to know, it makes sense now. I was looking for readonly (but didn't know it exists) so your first example |
The 3rd example errors for me // @type = { current: HTMLDivElement | undefined }
const refObjectYouMaintainToo = useRef<HTMLDivElement>() Error
code snippet (top component no conditional render) const myRef = useRef<HTMLDivElement>()
return (
<div className="App">
<div ref={myRef}></div> Maybe because I use CRA, which is a little behind |
updating typescript to 3.8.2 and @types/react to 16.9.21 didn't help |
If you're using a ref object to pass into a |
thanks for your explanation |
@thisconnect you are welcome to PR if you feel the cheatsheet could be improved to help you on this :) |
why not use like this " |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions! |
think we have addressed this well here https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/hooks/#useref |
What cheatsheet is this about? (if applicable)
How to use typescript and useRef
What's your issue or idea?
The following "works" / "typescript doesn't complain"... but is that the proper way?
The text was updated successfully, but these errors were encountered: