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

Bug: This isn't a bug but a feature request #20467

Closed
phtom opened this issue Dec 15, 2020 · 5 comments
Closed

Bug: This isn't a bug but a feature request #20467

phtom opened this issue Dec 15, 2020 · 5 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@phtom
Copy link

phtom commented Dec 15, 2020

React version: 17

This isn't a bug but a feature request that many devs would like to see in react including me.
For many times we have to use map funtion to loop items in a array just like the following:

exampleArray.map((index) => ( <motion.li key={index} className="item" variants={item} /> ))}

The sample above works great when we want to map an object, group of objects or arrays. There are some cases where we would like to do different. For example, in the code bellow I want inside JSX to loop 6 times a line of code:

{[0, 1, 2, 4, 5].map((index) => ( <motion.li key={index} className="item" variants={item} /> ))}

The code above seems off, we should be able to make a for cycle inside JSX. I would like to repeat the line 6 times to create 6 circles.

Do you think in future versions we could do it? Using this way doesn't seem great.

I wrote here because I didn't see any link pointing to feature or improvement request.

@phtom phtom added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Dec 15, 2020
@titivermeesch
Copy link

titivermeesch commented Dec 15, 2020

I feel like this is more something that should go here ?

It would feel a little weird and against the syntax in my opinion to have a for loop directly inside JSX (if that is what you are asking).

Did you thought about a syntax like this?

export default function App() {
  const elements = [];

  for (let i = 0; i < 5; i++) {
    elements.push(<div key={i}>Item</div>);
  }

  return <div>{elements}</div>;
}

@phtom
Copy link
Author

phtom commented Dec 15, 2020

I feel like this is more something that should go here ?

It would feel a little weird and against the syntax in my opinion to have a for loop directly inside JSX (if that is what you are asking).

Did you thought about a syntax like this?

export default function App() {
  const elements = [];

  for (let i = 0; i < 5; i++) {
    elements.push(<div key={i}>Item</div>);
  }

  return <div>{elements}</div>;
}

That would work too, I was skeptic to it too. I know it isn't best practices but sometimes it could be usefull.

@yume-chan
Copy link

There are so many ways to create a range.

My personal favorite is

Array(6).fill().map((_,index)=>index)

@phtom
Copy link
Author

phtom commented Dec 16, 2020

There are so many ways to create a range.

My personal favorite is

Array(6).fill().map((_,index)=>index)

It looks like a good solution. I still think that we should be able to loop with a for cycle inside JSX, sometimes it could make sense to use.

@bvaughn
Copy link
Contributor

bvaughn commented Dec 16, 2020

This isn't the right repo to discuss this feature request, but there is another RFC / PR open that's related that I'll link to:
facebook/jsx#99 (and its predecessor facebook/jsx#98)

Going to close this issue out and suggest moving the conversation over to the other PR.

@bvaughn bvaughn closed this as completed Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

4 participants