0

我有以下设置,但不断收到错误:Argument name clash. 我怎样才能解构这个并仍然保持价值?

const Message = ({message: { user, text }, nickname}, {quest: { user, text }, nickname} ) =>{ 
4

2 回答 2

0

您必须重命名“冲突”变量之一,如下所示:

const func = ({ a }, { a: b }) => {
  console.log(a, b);
}

const obj = { a: 'foo' };
func(obj, obj);

于 2020-04-06T18:33:51.713 回答
0

您可以重命名参数以解决变量名称冲突,例如:

const Message = ({
   message: {
      user: message_user,
      text: message_text
   },
   nickname: message_nickname
}, {
   quest: {
      user: quest_user,
      text: quest_text
   },
   nickname: quest_nickname
}) => {

}

或者,如果您想使用相同的变量名,也可以尝试此操作,但不能同时使用所有变量:

const Message = (param1, param2) => {
   let { message, nickname } = param1, quest;
   let { user, text } = message;
   // Do you stuff realed to message, user, text & nickname

   ({ quest, nickname } = param2);
   ({ user, text } = quest);
   // Do you stuff realed to quest, user, text & nickname
}
于 2020-04-06T18:34:26.327 回答