Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the `window` object of React synthetic?

Tags:

reactjs

I was wondering if the window object that is passed into react components synthetic or not.

If it isn't I am presuming I would need the following code to determine the width and height of the viewport

const w = window,
  d = document,
  documentElement = d.documentElement,
  body = d.getElementsByTagName('body')[0],
  width = w.innerWidth || documentElement.clientWidth || body.clientWidth,
  height = w.innerHeight || documentElement.clientHeight || body.clientHeight
like image 858
Archimedes Trajano Avatar asked Apr 05 '17 01:04

Archimedes Trajano


People also ask

What is window object React?

The window is the global object in the browser environment. Any property attached to the window object can be accessed from any script on the web page, including the script for the React app. Since window is a global object, the React code can also access its properties, as shown below.

What is synthetic React?

The react event handling system is known as Synthetic Events. The synthetic event is a cross-browser wrapper of the browser's native event. Handling events with react have some syntactic differences from handling events on DOM. These are: React events are named as camelCase instead of lowercase.

What are synthetic events used for React?

Synthetic events are that ReactJS reuses these events objects, by pooling them, which increase the performance.

Is preventDefault a synthetic event?

Both synthetic events and native events can implement the preventDefault and stopPropagation methods. However, synthetic events and native events are not exactly the same thing. For example, SyntheticEvent will point to mouseout for onMouseLeave Event.


2 Answers

The window object is the normal DOM object.

It is always available and safe to use (unless you are server side rendering).

like image 58
Raphael Rafatpanah Avatar answered Oct 20 '22 16:10

Raphael Rafatpanah


Accessing the window object in React is the same as anywhere else. It exists before any of your JavaScript runs. As a test, type about:blank into your location bar, open the console and type 'window'. It's there even with no other JavaScript on the page.

like image 29
Jim Perris Avatar answered Oct 20 '22 17:10

Jim Perris