5

This seems like a stupid question. Say I have a function which accepts an object. How can I cast that object as props, but also destructure props.id to id (in the parameter declaration)?

function go ({ id }) {
  const props = arguments[0]; // how to do this with destructure?
  console.log('props', props, 'id', id);
}

go({id: 2});
neaumusic
  • 10,027
  • 9
  • 55
  • 83

2 Answers2

6

You can't do it - just keep props as an argument as well to make this code simpler and easier to read:

function go (props) {
  const { id } = props;
  console.log('props', props, 'id', id);
}

go({id: 2});
neaumusic
  • 10,027
  • 9
  • 55
  • 83
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
3

You can follow this approach which names a param as props and destructures that param to extract the value of Id.

The problem comes when you need to pass an additional param.

function go (props, {id} = props) {
  //const props = arguments[0]; // how to do this with destructure?
  console.log('props', props, 'id', id);
}

go({id: 2});
Ele
  • 33,468
  • 7
  • 37
  • 75