Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution to unique key problem. #64

Closed
wants to merge 1 commit into from
Closed

Conversation

daliusd
Copy link
Contributor

@daliusd daliusd commented Oct 8, 2021

We have bunch of warnings in our code that looks like this:

Warning: Each child in a list should have a unique "key" prop.

Check the top-level render call using <u>. See https://reactjs.org/link/warning-keys for more information.

It looks like those warnings are coming from mjml-react because children are passed as third argument to React.createElement. This is most probably wrong. E.g.:

const a = [1, 2];

function hello() {
  return (
    <div>
      {a.map((i) => (<span>{i}</span>))}
  	</div>
  );
}


function hello2() {
  return (
    <div>
      <span>1</span>
      <span>2</span>
  	</div>
  );
}

JSX is transformed differently for these two cases:

const a = [1, 2];

function hello() {
  return /*#__PURE__*/React.createElement("div", null, a.map(i => /*#__PURE__*/React.createElement("span", null, i)));
}

function hello2() {
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", null, "1"), /*#__PURE__*/React.createElement("span", null, "2"));
}

It seems, if children is passed as third argument when it is Array, then it is treated as result of Array.map and React issues warning about missing key.

This fix checks if children is Array and in case it is spreads is as arguments to React.createElement.

@daliusd
Copy link
Contributor Author

daliusd commented Oct 11, 2021

Problem was in different location.

@daliusd daliusd deleted the unique-key-problem branch October 11, 2021 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant