user14587589
user14587589

Reputation: 459

React-native useRef value is giving undefined

I am trying to use useRef on my TextInput like this:

   <TextInput
            ref={commentRef}
            defaultValue={`${postComment}`}
            placeholder={
              addLinkToArticleStatus
                ? 'Attach link to an existing article'
                : 'Comment or ask specific question'
            }
            placeholderTextColor="gray"
          />

I have function which shall read the value from the ref. And for now I am doing it like this

const addNewComment = () => {
    console.log('--commentRef--', commentRef.current.value);
  };

But It is always giving me undefined and I don't understand why. Any suggestions please?

Upvotes: 0

Views: 142

Answers (1)

D10S
D10S

Reputation: 1549

Try initialize it with null. i.e:

const commentRef = React.useRef(null);

Or use createRef:

const commentRef = React.createRef();

Upvotes: 1

Related Questions