Home > database >  How to set destructuring values?
How to set destructuring values?

Time:06-17

I have this:

  const { 
    user,
    id,
    name,
    desc,
    photos,
    video,
    price,
  } = route.params;

  <ProductFooter 
    selectedOptions={selectedOptions}
    {...route.params}
  />

Now I want to get the user in a variable and the other props in another variable

const ProductFooter = ({...props}) => {
  const { t } = useTranslation();
  const dispatch = useDispatch();

  const user = the user props,
  const product = the other props

How can I set now the values ?

CodePudding user response:

You can do it like this using ... operator. That helps in object/array destructuring.

const obj ={ 
a : 1, b: 2, c : 3, user : 'joe' };

const { user, ...rest } = obj;
console.log(user);
console.log(rest);

CodePudding user response:

You can destructure your props object like this, and just assign the ...rest :

const ProductFooter = ({ user, ...rest }) => {
  const { t } = useTranslation();
  const dispatch = useDispatch();

  const product = rest;

Don't forget you are also passing selectedOptions which will be assigned with ...rest unless you separate it out like with user.

  • Related