Enable redux-form and set focus to field

≯℡__Kan透↙ 提交于 2019-12-08 13:05:35

问题


I've got disabled form field by default and when user clicks on button Edit, it will enable the field. I also want to set focus on the field, but I don't know how. I've created ref to the field and tried some focus() function on it, but it hasn't worked. You can try it here on sandbox.


回答1:


  1. You must assign the ref to the input element instead of the Field component
  2. Add an callback to set in your parent component the reference of subcomponent input
  3. Use the reference to focus the input when pressing the edit button:

https://codesandbox.io/s/rlx390xn6m




回答2:


just HTML's autofocus is much better: https://davidwalsh.name/autofocus

  • in react use with camelcase: autoFocus


来源:https://stackoverflow.com/questions/51267365/enable-redux-form-and-set-focus-to-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!