使用样式的组件中的反应对react中的文本输入进行自动对焦

发布于 2025-02-10 19:32:02 字数 646 浏览 1 评论 0原文

如何在页面加载上使用样式的组件进行React中的TextInput?

我确实使用了usefect,但看来它没有在文本输入上自动对焦。我希望将电子邮件文本输入重点放在页面加载上。

codesandbox->

function App() {
  const emailInput = useRef(null);

  useEffect(() => {
    if (emailInput.current) {
      emailInput.current.focus();
    }
  }, []);

  return (
    <div>
      <Input type="text" placeholder="Name" />
      <Input type="email" placeholder="Email" innerRef={emailInput} />
    </div>
  );
}

How do you make the TextInput in React using Styled Components to be autoFocus on page load?

I did using useEffect but it seems it doesn't autoFocus on the TextInput. I wanted the email text input to be focus outlined on page load.

Codesandbox -> CODESANDBOX

function App() {
  const emailInput = useRef(null);

  useEffect(() => {
    if (emailInput.current) {
      emailInput.current.focus();
    }
  }, []);

  return (
    <div>
      <Input type="text" placeholder="Name" />
      <Input type="email" placeholder="Email" innerRef={emailInput} />
    </div>
  );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2025-02-17 19:32:02

按照样式components文档,在 InnerRefInnerRef被弃用为V4:

注意

在样式组件V4中除去了“ InterRef”支柱,而有利于React 16 ForwardRef API。只需使用普通的裁判道具即可。

在React 16之前,没有一个很好的方法将参考文献传递给父母(使用回调除外)。在react 16.3 forward> forward> forward> forward> forward> useref honk稍后。样式的组件V4已开始使用该机制。

因此,您可以直接使用Ref

function App() {
  const emailInput = useRef(null);

  useEffect(() => {
    if (emailInput.current) {
      emailInput.current.focus();
    }
  }, []);

  return (
    <div>
      <Input type="text" placeholder="Name" />
      <Input type="email" placeholder="Email" ref={emailInput} />
    </div>
  );
}

在另一侧,如果目的仅用于聚焦,则如@andy所述,autofocus属性可能是一个更好的选择:

function App() {
  return (
    <div>
      <Input type="text" placeholder="Name" />
      <Input type="email" placeholder="Email" autoFocus />
    </div>
  );
}

React的autocus属性正在与上面的代码完全相同(focus在安装上以编程方式元素)。请勿将其与HTML的自动对焦属性混淆,该行为在浏览器之间不一致。

As per styled-components documentation, in innerRef section, the innerRef is deprecated as of v4:

NOTE

The "innerRef" prop was removed in styled-components v4 in favor of the React 16 forwardRef API. Just use the normal ref prop instead.

Before React 16, there was no nice way to pass references up to the parent (except using callbacks). In React 16.3 createRef and forwardRef have been introduced for doing that, along with useRef hook later. Styled Components v4, has started to use that mechanism.

Therefore, you may use ref directly:

function App() {
  const emailInput = useRef(null);

  useEffect(() => {
    if (emailInput.current) {
      emailInput.current.focus();
    }
  }, []);

  return (
    <div>
      <Input type="text" placeholder="Name" />
      <Input type="email" placeholder="Email" ref={emailInput} />
    </div>
  );
}

On the other side, if the intent is only for focusing, as stated by @Andy, the autoFocus property may be a better choice:

function App() {
  return (
    <div>
      <Input type="text" placeholder="Name" />
      <Input type="email" placeholder="Email" autoFocus />
    </div>
  );
}

The React's autoFocus attribute is doing exactly the same thing as the code above (focus programmatically element on the mount). Do not confuse it with HTML's autofocus attribute, which behavior is inconsistent between browsers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文