React useRef()

HyosikPark·2020년 9월 26일
0

const commentUnputRef = useRef()

const [submitComment] = useMutation(SUBMIT_COMMENT_MUTATION, {
update() {
setComment('');
commentInputRef.current.blur();
},
variables: {
postId,
body: comment,
},
});

<input
type='text'
placeholder='Comment..'
name='comment'
value={comment}
onChange={(event) => setComment(event.target.value)}
ref={commentInputRef}
/>
<button
type='submit'
className='ui button teal'
disabled={comment.trim() === ''}
onClick={submitComment}

처음으로 useRef를 사용했다. 가장 많이 사용되는 DOM에 접근하는 방식인데.
어떠한 동작을 실행한 뒤 input값에 focus, blur 등의 처리를 하는 방법이다.

0개의 댓글