dai-shi / react-suspense-fetch

[NOT MAINTAINED] A low-level library for React Suspense for Data Fetching
MIT License
296 stars 8 forks source link

Object.keys vs Object.getOwnPropertyNames #2

Closed jimthedev closed 4 years ago

jimthedev commented 4 years ago

Hey there.

Lines 34: https://codesandbox.io/s/optimistic-nightingale-m617m

You can see object.keys shows as an empty array. Any ideas?

dai-shi commented 4 years ago

Confirmed the issue. Thanks.

I think it's because we pass {} to Proxy. https://github.com/dai-shi/react-suspense-fetch/blob/6ec6369949896b31f9fec60ef71edc0ff6b5db75/src/index.ts#L136

I expected that Object.key is also trapped by ownKeys but maybe not. Hm, not sure how to deal with it.


If this {} is the cause, this issue won't affect reactive-react-redux nor react-tracked, because they have the original object.

dai-shi commented 4 years ago
> p = new Proxy({ a: 1 }, { ownKeys() { console.log('ownKeys'); return ['b']; } });
Proxy {a: 1}
> Object.keys(p)
ownKeys
[]
> Object.getOwnPropertyNames(p)
ownKeys
["b"]

OK, it's trapped, but returning []. How come. Hm, now wondering why it works in reactive-react-redux.

dai-shi commented 4 years ago

OK, Object.keys only lists enumerables. So, we need to trap getOwnPropertyDescriptor too.

dai-shi commented 4 years ago

Published: https://www.npmjs.com/package/react-suspense-fetch/v/0.1.0

dai-shi commented 4 years ago

https://codesandbox.io/s/hungry-merkle-fnri1 It's fixed.

Thanks for reporting!

jimthedev commented 4 years ago

Thanks it is working great!