使用React Material-UI自动对焦TextField

Amr*_*man 2 reactjs material-ui

我使用Material-UI TextField

我想实现一个自动对焦,我无法通过设置autofocus=true而不是以编程方式从标记中找到方法.任何帮助?

Dei*_*kas 12

你可以这样做:<TextField autoFocus></TextField>.

更多相关信息:https://facebook.github.io/react/docs/tags-and-attributes.html#html-attributes.


Lan*_*tig 9

由于某种原因,这对我不起作用(可能是因为它位于安装顶级组件时不可见的组件内)。我必须做一些更复杂的事情才能让它发挥作用:

const focusUsernameInputField = input => {
  if (input) {
    setTimeout(() => {input.focus()}, 100);
  }
};

return (
  <TextField
    hintText="Username"
    floatingLabelText="Username"
    ref={focusUsernameInputField}
  />
)
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅https://github.com/callemall/material-ui/issues/1594