Skip to content

Instantly share code, notes, and snippets.

@OnkelTem
Last active July 24, 2019 12:11
Show Gist options
  • Save OnkelTem/8b8b4d8c505b768575c1ad92e5586adc to your computer and use it in GitHub Desktop.
Save OnkelTem/8b8b4d8c505b768575c1ad92e5586adc to your computer and use it in GitHub Desktop.
[REACT] Map gives its closure, not need to provide more
// Variant 1
const ItemList = ({items, editItem}) => {
render (
<div>
items.map(item => (
<div onClick={(item => () => { editItem(item); })(item)}></div>
)
</div>
);
}
// Variant 2
const ItemList = ({items, editItem}) => {
const openEditForm = item => () => {
editItem(item);
}
render (
<div>
items.map(item => (
<div onClick={() => { openEditForm(item); }}></div>
)
</div>
);
}
// Variant n+1 from the comments
const ItemList = ({items, editItem}) => {
render (
<div>
{items.map(item => <div onClick={() => editItem(item)} />)}
</div>
);
}
@efdee
Copy link

efdee commented Jul 24, 2019

// Variant 3
const ItemView => ({item, editItem}) => {
  render (
    <div onClick={() => editItem(item)}></div>
  );
}

@efdee
Copy link

efdee commented Jul 24, 2019

const ItemList = ({items, editItem}) => {
  render (
    <div>
      {items.map(item => <div onClick={() => editItem(item)} />)}
    </div>
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment