Skip to content

how to check if object is proxy or not? #473

Closed Answered by dai-shi
aelbore asked this question in Q&A
Discussion options

You must be logged in to vote

In general, I would suggest to avoid the need of checking if an object is a proxy at runtime. You can't call useSnapshot conditionally anyway.
If you need dynamic coding style, you want to have such a property on your end.

import { proxy } from 'valtio'

const IS_PROXY = Symbol()

const state = proxy({
  // ...
  [IS_PROXY]: true,
})

const isProxy = (obj) => obj[IS_PROXY] === true

For hacks and probably for 3rd-party libraries, this is possible:

import { getVersion } from 'valtio'

const isValtioProxy = (obj) => typeof getVersion(obj) === 'number'

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@aelbore
Comment options

Answer selected by aelbore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants