Skip to content

programatically set focus #1225

Answered by fridaystreet
fridaystreet asked this question in Q&A
Discussion options

You must be logged in to vote

managed to get something working, but this feels like a total hack. Would be great if someone can point me in a better direction.
basically using state to set the autoFocus property of editableProps. When I want to refocus on the editor, reset the property to false, then settimeout to set it back to true. Then select all and collapse to the end through a transform. I store the current value in the contentRef and pass that to editor via the initialValue so that it rerenders from changing the autofocus prop with previous value before doing the transform.

in very basic form:

 const contentRef = useRef();

  const focusEditor = useCallback(() => {
    setAutoFocus(false);
    setTimeout(() =>…

Replies: 4 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by zbeyens
Comment options

You must be logged in to vote
1 reply
@kungfoolabs
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@kumarajay0412
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants