UI developer tutorials and guides

Friday, June 24, 2022

How to set focus to a input in Solidjs

When we required to access the element in the DOM we can use the Ref. It is similar to the Ref in React.

Suppose we have a input text box deeper in the return statement and want to set focus when it rendered, then Ref become handy 🖐.

Here is the quick example

function Component(){ let myBox;
setTimeout(() => myBox.focus());
return(<> <div> <input ref={myBox} type="text"/></div> </>)}

Here in the Solidjs component example we done the following

  • created a variable myBox
  • Set ref to input box - ref={myBox}
  • Accessing the ref element and set focus to the input.
     


0 comments:

Post a Comment