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

前端 未结 1 1042
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-29 14:08

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

How to get these to be recognized as valid?
C

相关标签:
1条回答
  • 2021-01-29 14:24

    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.

    0 讨论(0)
提交回复
热议问题