如何以编程方式对焦点输入做出反应
how react programmatically focus input
我正在尝试实现一个非常简单的用例,一个 UI 功能,其中:
- 有一个标签,里面有一些内容
- 如果单击,文本输入会将其替换为可用标签的内容
- 用户可以编辑内容
- 按下回车键时,输入隐藏,标签返回更新后的内容
我终于可以完全正确(实际上有 MongoBD 后端、redux 等),而我唯一做不到的事情(花一整天的时间在谷歌搜索和阅读 S.O.F 类似帖子)是这样的:
当我的文本输入出现时,我无法将焦点转移到它!首先我这样累:
<div className={((this.state.toggleWordEdit) ? '' : 'hidden')}>
<input id={this.props.word._id} className="form-control"
ref="updateTheWord"
defaultValue={this.state.word}
onChange={this.handleChange}
onKeyPress={this.handleSubmit}
autoFocus={this.state.toggleWordEdit}/></div>
<div className={((this.state.toggleWordEdit) ? 'hidden' : '')}>
<h3 onClick={this.updateWord}>
{this.state.word}</h3>
</div>
但 autoFocus 确实不起作用(我 "guess" 因为表单已呈现,但处于隐藏状态,使 autoFocus 无用)。
接下来我在 this.updateWor 中尝试了很多我在 google 和 S.O.F 上找到的建议。:
this.refs.updateTheWord.focus();
连同类似的建议都没有用。我还试图愚弄 React,看看我是否能做点什么!我用的是真实的 DOM:
const x = document.getElementById(this.props.word._id);
x.focus();
也没有用。我什至无法理解的一件事是这样的建议:
我什至没有尝试过,因为我有多个这些组件,我需要 ref 来进一步获取每个组件的价值,而且我无法想象如果我的 ref 没有命名,我如何获得 ![= 的价值14=]
所以你能不能给我一个想法,帮助我理解如果我不使用表单(因为我需要一个输入框来替换标签)我如何在 CSS (Bootstrap) class 输了 'hidden' 好吗?
您使用 refs 的方式不是最喜欢的方式,否则它不再是最佳做法。尝试这样的事情
class MyClass extends React.Component {
constructor(props) {
super(props);
this.focus = this.focus.bind(this);
}
focus() {
this.textInput.current.focus();
}
render() {
return (
<div>
<input
type="text"
ref={(input) => { this.textInput = input; }} />
<input
type="button"
value="Set Focus"
onClick={this.focus}
/>
</div>
);
}
}
更新
从 React 16.3 开始,您可以使用 React.createRef()
API
class MyClass extends React.Component {
constructor(props) {
super(props);
// create a ref to store the textInput DOM element
this.textInput = React.createRef();
this.focus = this.focus.bind(this);
}
focus() {
// Explicitly focus the text input using the raw DOM API
// Note: we're accessing "current" to get the DOM node
this.textInput.current.focus();
}
render() {
// tell React that we want to associate the <input> ref
// with the `textInput` that we created in the constructor
return (
<div>
<input
type="text"
ref={this.textInput} />
<input
type="button"
value="Set Focus"
onClick={this.focus}
/>
</div>
);
}
}
使用componentDidUpdate
方法每次更新组件
componentDidUpdate(prevProps, prevState) {
this.input.focus();
}
只需将 autofocus 属性添加到 input
。 (当然在JSX
是autoFocus
)
<input autoFocus ...
除了之前的答案,我还添加了 setTimeout
以使其生效
handleClick() {
if (this.searchInput) {
setTimeout(() => {
this.searchInput.focus();
}, 100);
}
}
其中 searchInput
是输入的 jsx 引用
<input
type="text"
name="searchText"
ref={(input) => { this.searchInput = input; }}
placeholder="Search" />
并且 handleClick()
是任何元素的 onClick
处理程序
使用焦点挂钩
// General Focus Hook
const useFocus = (initialFocus = false, id = "") => {
const [focus, setFocus] = useState(initialFocus)
const setFocusWithTrueDefault = (param) => setFocus(isBoolean(param)? param : true)
return ([
setFocusWithTrueDefault, {
autoFocus: focus,
key: `${id}${focus}`,
onFocus: () => setFocus(true),
onBlur: () => setFocus(false),
},
])
}
const FocusDemo = () => {
const [labelStr, setLabelStr] = useState("Your initial Value")
const [setFocus, focusProps] = useFocus(true)
return (
<> {/* React.Fragment */}
<input
onChange={(e)=> setLabelStr(e.target.value)}
value={labelStr}
{...focusProps}
/>
<h3 onClick={setFocus}>{labelStr}</h3>
</>
)
}
要获得更完整的演示 click here。
@BenCarp 打字稿的回答
将 inputRef
传递给 input
并调用 setFocus
将焦点设置到它。
export const useInputFocus = (): [MutableRefObject<HTMLInputElement | undefined>, () => void] => {
const inputRef = useRef<HTMLInputElement>();
const setFocus = (): void => {
const currentEl = inputRef.current;
if (currentEl) {
currentEl.focus();
}
};
return [inputRef, setFocus];
};
您可以使用“useRef”挂钩并引用您的输入控件,然后使用您的 reference.current.focus()
你可以看视频看例子:
我正在尝试实现一个非常简单的用例,一个 UI 功能,其中:
- 有一个标签,里面有一些内容
- 如果单击,文本输入会将其替换为可用标签的内容
- 用户可以编辑内容
- 按下回车键时,输入隐藏,标签返回更新后的内容
我终于可以完全正确(实际上有 MongoBD 后端、redux 等),而我唯一做不到的事情(花一整天的时间在谷歌搜索和阅读 S.O.F 类似帖子)是这样的:
当我的文本输入出现时,我无法将焦点转移到它!首先我这样累:
<div className={((this.state.toggleWordEdit) ? '' : 'hidden')}>
<input id={this.props.word._id} className="form-control"
ref="updateTheWord"
defaultValue={this.state.word}
onChange={this.handleChange}
onKeyPress={this.handleSubmit}
autoFocus={this.state.toggleWordEdit}/></div>
<div className={((this.state.toggleWordEdit) ? 'hidden' : '')}>
<h3 onClick={this.updateWord}>
{this.state.word}</h3>
</div>
但 autoFocus 确实不起作用(我 "guess" 因为表单已呈现,但处于隐藏状态,使 autoFocus 无用)。
接下来我在 this.updateWor 中尝试了很多我在 google 和 S.O.F 上找到的建议。:
this.refs.updateTheWord.focus();
连同类似的建议都没有用。我还试图愚弄 React,看看我是否能做点什么!我用的是真实的 DOM:
const x = document.getElementById(this.props.word._id);
x.focus();
也没有用。我什至无法理解的一件事是这样的建议:
所以你能不能给我一个想法,帮助我理解如果我不使用表单(因为我需要一个输入框来替换标签)我如何在 CSS (Bootstrap) class 输了 'hidden' 好吗?
您使用 refs 的方式不是最喜欢的方式,否则它不再是最佳做法。尝试这样的事情
class MyClass extends React.Component {
constructor(props) {
super(props);
this.focus = this.focus.bind(this);
}
focus() {
this.textInput.current.focus();
}
render() {
return (
<div>
<input
type="text"
ref={(input) => { this.textInput = input; }} />
<input
type="button"
value="Set Focus"
onClick={this.focus}
/>
</div>
);
}
}
更新
从 React 16.3 开始,您可以使用 React.createRef()
API
class MyClass extends React.Component {
constructor(props) {
super(props);
// create a ref to store the textInput DOM element
this.textInput = React.createRef();
this.focus = this.focus.bind(this);
}
focus() {
// Explicitly focus the text input using the raw DOM API
// Note: we're accessing "current" to get the DOM node
this.textInput.current.focus();
}
render() {
// tell React that we want to associate the <input> ref
// with the `textInput` that we created in the constructor
return (
<div>
<input
type="text"
ref={this.textInput} />
<input
type="button"
value="Set Focus"
onClick={this.focus}
/>
</div>
);
}
}
使用componentDidUpdate
方法每次更新组件
componentDidUpdate(prevProps, prevState) {
this.input.focus();
}
只需将 autofocus 属性添加到 input
。 (当然在JSX
是autoFocus
)
<input autoFocus ...
除了之前的答案,我还添加了 setTimeout
以使其生效
handleClick() {
if (this.searchInput) {
setTimeout(() => {
this.searchInput.focus();
}, 100);
}
}
其中 searchInput
是输入的 jsx 引用
<input
type="text"
name="searchText"
ref={(input) => { this.searchInput = input; }}
placeholder="Search" />
并且 handleClick()
是任何元素的 onClick
处理程序
使用焦点挂钩
// General Focus Hook
const useFocus = (initialFocus = false, id = "") => {
const [focus, setFocus] = useState(initialFocus)
const setFocusWithTrueDefault = (param) => setFocus(isBoolean(param)? param : true)
return ([
setFocusWithTrueDefault, {
autoFocus: focus,
key: `${id}${focus}`,
onFocus: () => setFocus(true),
onBlur: () => setFocus(false),
},
])
}
const FocusDemo = () => {
const [labelStr, setLabelStr] = useState("Your initial Value")
const [setFocus, focusProps] = useFocus(true)
return (
<> {/* React.Fragment */}
<input
onChange={(e)=> setLabelStr(e.target.value)}
value={labelStr}
{...focusProps}
/>
<h3 onClick={setFocus}>{labelStr}</h3>
</>
)
}
要获得更完整的演示 click here。
@BenCarp 打字稿的回答
将 inputRef
传递给 input
并调用 setFocus
将焦点设置到它。
export const useInputFocus = (): [MutableRefObject<HTMLInputElement | undefined>, () => void] => {
const inputRef = useRef<HTMLInputElement>();
const setFocus = (): void => {
const currentEl = inputRef.current;
if (currentEl) {
currentEl.focus();
}
};
return [inputRef, setFocus];
};
您可以使用“useRef”挂钩并引用您的输入控件,然后使用您的 reference.current.focus()
你可以看视频看例子: