Javascript 光标位于带有React的自动聚焦文本区域的开头

Javascript 光标位于带有React的自动聚焦文本区域的开头,javascript,html,reactjs,Javascript,Html,Reactjs,我正在使用一个文本区域,该文本区域在安装时具有自动对焦功能,并使用React,以便将光标放置在文本区域的最末端。如何将光标放在最开始的位置(请参见下面的代码片段)?谢谢 类应用程序扩展了React.Component{ componentDidMount(){ this.textArea.focus(); } render(){ 返回( this.textArea=node} value=“您好,我希望焦点时光标位于开头(在您好之前),而不是结尾。” /> ); } } ReactDOM.re

我正在使用一个文本区域,该文本区域在安装时具有自动对焦功能,并使用React,以便将光标放置在文本区域的最末端。如何将光标放在最开始的位置(请参见下面的代码片段)?谢谢

类应用程序扩展了React.Component{
componentDidMount(){
this.textArea.focus();
}
render(){
返回(
this.textArea=node}
value=“您好,我希望焦点时光标位于开头(在您好之前),而不是结尾。”
/>
);
}
}
ReactDOM.render(,document.getElementById('app'))

当焦点事件触发时,可以使用该属性并将其设置为0

类应用程序扩展了React.Component{
手焦点(e){
const target=e.target;
setTimeout(()=>e.target.selectionEnd=0,0);
}
render(){
返回(
);
}
}
ReactDOM.render(,document.getElementById('app'))

多亏了加布里埃尔·彼得里奥利,答案如下:

类应用程序扩展了React.Component{
componentDidMount(){
this.textArea.selectionEnd=0;
this.textArea.focus();
}
render(){
返回(
this.textArea=node}
value=“您好,我希望焦点时光标位于开头(在您好之前),而不是结尾。”
/>
);
}
}
ReactDOM.render(,document.getElementById('app'))


对不起,请参见编辑,我想问这个问题,关于在装载时文本区域何时自动聚焦。@吉米,您是否在调用
之前或之后尝试设置
this.textArea.selectionEnd=0
。this.textArea.focus()