Search code examples
javascriptreactjsreact-state-management

Is it possible to get an actual Javascript object from hookstate.js State without referencing the objects keys?


I am using the hookstate.js library and am trying to something as simple as to get the entire Javascript object from the State object without having to reference the key of the object.

For example, state.set({first:'John',last:'Doe'}) and then somehow access the entire object (as a bona fide JS object, not State) without having to know the object key and reference state.first.get().
Is there no built in way to do such a simple thing?

I can do so with a reduce:

const { keys } = state
const jsObject= keys.reduce((pre, cur) => {
const stateVal = state[cur].get()
     return { ...pre, [cur]: stateVal }
}, {})

However, this is not possible if I am expecting nested objects.


Solution

  • Author of Hookstate here. You can call State.get() on the root of the state and use noproxy and stealth options (since Hookstate 4 version) for the get method. It will do what you require.