score:1

Accepted answer

notes:

1. this code will error if sessiondata is null or undefined:

const [{sessiondata: { user }}, loading] = usesession();

const usesession = () => [{
  expires: new date(date.now() + (3600 * 1000 * 24)),
  sessiondata: null
}, false];

const [{ sessiondata: {user}}, loading] = usesession();
console.log('user:', user, 'loading:', loading);

2. using the same code, you can't assign an inline default value to fix the typeerror described above.

const somedata = {
  foo: 'foo',
  bar: 'bar'
};

//assign `baz: 'default'` to avoid `typeerror` if `baz` property does not exist
const {foo, bar, baz = 'default'} = somedata;
console.log(foo, bar, baz);



//sadly, this doesnt work here.
const usesession = () => [{
  expires: new date(date.now() + (3600 * 1000 * 24)),
  sessiondata: null
}, false];

const [{ sessiondata: {user} = {} }, loading] = usesession();
console.log('user:', user, 'loading:', loading);

maybe i'm missing something but i just can't get it to work using default values. the only way i fixed this is getting sessiondata first then work on that afterwards while using default values.

const usesession = () => [{
  expires: new date(date.now() + (3600 * 1000 * 24)),
  sessiondata: null
}, false];

//assign {} to `sessiondata` if it is undefined
const [{expires, sessiondata = {} }, loading] = usesession();
console.log(sessiondata); //null

//above default value will still fail if sessiondata is null.
// to fix, coalesce to {}
const {user, token} = sessiondata || {}; 
console.log(user, token);

3. be careful with "gotcha's"

you may have noticed the || {} code in the above code even though we added a default value to sessiondata. this is because default values only apply to undefined, not nulls.

on the same note, it is better to add a coalesce on usesession too since your code will break if usesession() returns a null or undefined.

const usesession = () => { return null; };

//assign {} to `sessiondata` if it is undefined
console.log(usesession()); //null

// usesession()[0], a.k.a. session is undefined
// need to add default since we are destructuring `sessiondata` from `session`
const [{ sessiondata } = {}, loading] = usesession() || [];

//// this also works but it assigns `sessiondata = {}`
//const [{ sessiondata = {} } = {}, loading] = usesession() || [];
// wont be doing that since we are proving the next point:

//above default value will still fail since `sessiondata` will be undefined
// to fix, coalesce to {}
const {user, token} = sessiondata || {}; 
console.log(user, token);

finally

getting sessiondata and/or expires (a.k.a session) first actually isn't bad at all since you may also want to get the other properties, i.e, session.expires, sessiondata.token, etc. it also allows you to fix the gotchas mentioned above.

this awesome article (object destructuring best practice in javascript) discusses the above notes in-depth. take time to read.

score:0

the object structure you provided is not valid. i came to get user and token with following construct:

var a = {
      session: {
        expires: '1d',
        sessiondata: {
          user: 'myuser',
          token: 'token'
        }
      }
    }
    
    var {session: {sessiondata: {user, token}, expires}} = a
    
    console.log(user, token, expires) // 'myuser token 1d'

for more info, see es6 deep nested object destructuring

score:1

you can destructure like this to set the user variable:

const [{sessiondata: { user }}, loading] = usesession();

Related Query

More Query from same tag