How to get VScode to recognize history and document in react js apps

可紊 提交于 2021-02-05 12:29:34

问题


VScode is highlighting lines that use document and history as undefined per my linter rules.

How to get these to be recognized as valid?
Currently I am disabling no-undef (at the line level) but that is obviously a hack.


回答1:


You can use the globals directive to specify variables which are defined globally.

However, you shouldn't need to access document or history directly in a React app in the first place.

Generally you should interact with the history only through the use of a router, and you should avoid direct DOM access entirely.

The main things you want want to use direct DOM access for are changing things in the <head> which are better handled with React Helmet or for manipulating content of a component which should almost always be done through state and props and where the rare exceptions should be handled via a useRef hook instead of searching the document via document.querySelector or similar.



来源:https://stackoverflow.com/questions/65233936/how-to-get-vscode-to-recognize-history-and-document-in-react-js-apps

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