Sahil Jain
Sahil Jain

Reputation: 3809

Hide state structure in production

I am using Provider from react-redux. In production, I can see the state structure if I have the react chrome extension installed using $r.store.getState(). How do I hide this behavior in production ?

Upvotes: 3

Views: 956

Answers (1)

1ven
1ven

Reputation: 7026

You can make global $r variable not writable, using Object.defineProperty function:

Object.defineProperty(window, '$r', {
  writable: false
});

In that case, React Devtools can't assign a value to $r variable and therefore no one can access store instance using React Devtools.

Edit:

When setting writable: false property, React Devtools throws an error, that $r property is not writable. In order to avoid that error, you can set constant value to that variable that way:

Object.defineProperty(window, '$r', {
  value: {}
});

In that case, $r value will be always equal to {} and console will be silent.

Upvotes: 3

Related Questions