score:2

Accepted answer

in case that the property you are destructuring is not defined, you can assign "default values" like this:

const props = {diffuser: {name: "peter"}};
const { user: {name} = {name: "default-value"} } = props;
console.log(name);

the simpler example,

var { message: msg = "something went wrong" } = {};
console.log(msg);

a variable can be assigned a default, in the case that the value unpacked from the object is undefined.

score:0

you can just add a quick default value an check for undefined or null string after:

const { user: { name } = {} } = props;

this way it will not throw an error if 'user' is undefined, name will just be undefined also.

score:3

try this instead:

const {
    user: { name = '' } = {name: ''}
  } = props;

<div>{name}</div>

Related Query

More Query from same tag