How to fix losing focus after 1 character with React-Router

半城伤御伤魂 提交于 2020-03-05 02:42:17

问题


An input element loses focus after typing one character. This SO question has some answers, suggesting to add the id or key property, however, I could not fix my problem this way. Then I pinpointed the problem to react-router-dom. This has the problem:

<Route path='xxx' component={() => <TheComponent... />}

where TheComponent contains the input element that loses focus while typing. This did not fix the problem:

<Route id='1' path='xxx' component={() => <TheComponent... />}

Changing the id to key did also not work. But this did fix it:

<Route path='xxx'>
  <TheComponent... />
</Route>

Searching for this problem of losing focus related to react-router, I found many SO questions, e.g. this question, while many questions have either no answer, or an answer that does not give me a clue to a good solution.

Can anyone explain why the last option solves the problem, and why it is not possible to use the router construction in the first code example?

来源:https://stackoverflow.com/questions/59577900/how-to-fix-losing-focus-after-1-character-with-react-router

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