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

Avoiding repetition #22

Open
oliverjam opened this issue Sep 28, 2018 · 0 comments
Open

Avoiding repetition #22

oliverjam opened this issue Sep 28, 2018 · 0 comments

Comments

@oliverjam
Copy link

Since you have the power of JS at your disposal you can avoid repeating yourself too much to render identical copies of a component.

It's easy if you have an array of data to map over (array.map(thing => <div>{thing}</div>), but here's a trick even if you don't:

Array.from({ length: 9 }, (_, index) => (
  <Btn
    key={`${button}-index`} // not ideal but there's nothing else unique to use
    zombiesAlive={this.state.zombiesAlive}
    increment={this.increment}
    decrement={this.decrement}
    firstZombieAppeared={this.setFirstZombieAppeared}
  />
))

Array.from takes an object with a length as the first argument and a map function as the second. This produces an array of 9 buttons for React to render

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

No branches or pull requests

2 participants