Next.js Discord

Discord Forum

Having issue with useRef

Unanswered
Great Skua posted this in #help-forum
Open in Discord
Great SkuaOP
I have useRef
const newWishlistInputRef = useRef<HTMLInputElement | null>(null);

and I have this function
const submitNewTaskHandler = (event: FormEvent) => {
    event.preventDefault();

    if (newWishlistInputRef.current) {
      addWishlistToArr({
        id: nextId,
        text: capitalizeFirstCharacter(newWishlistInputRef.current.value),
        isEditMode: false
      })
      newWishlistInputRef.current.value = '';
      newWishlistInputRef.current.focus();
    }
  }

Why I can just use the newWishlistInputRef.current.value without the if (newWishlistInputRef.current) {?
It says 'newWishlistInputRef.current' is possibly 'null'.ts(18047)

0 Replies