0

I am trying to display 5 input fields on the screen within a for loop. I also want to apply a dynamic class to each of them later (according to their state). Here is my code:

function App() {
    ...

    return (
        <Grid container className={classes.root}>
            <CssBaseline />
            <Grid item xs={false} md={7} className={classes.image} />
            <Grid item xs={12} md={5} component={Paper} elevation={6} square>
                <div className={classes.paper}>
                    <form className={classes.form} onSubmit={handleSubmit}>
                        {inputFields}
                        for (let i = 0; i < 5; i++) {
                            <TextField />
                        }
                        {for (let i = 0; i < 5; i++) {
                            <TextField />
                        }}
                        <Button
                            type="submit"
                            fullWidth
                            variant="contained"
                            color="primary"
                            className={classes.submit}
                        >
                            Submit
                        </Button>
                    </form>
                </div>
            </Grid>
        </Grid>
    );
}

Here is the error I am getting: SyntaxError: Unexpected token.

enter image description here

enter image description here

I am still learning React, so I am pretty sure I am doing it wrong. How does one render multiple elements with a for loop? Is there a better way?

Thanks!

5
  • 1
    for(){} is JS code. To embed JS code in JSX, you must put it inside two curly braces such as: {for (){}} Commented Apr 4, 2022 at 13:51
  • Thanks, but I get the same error (Unexpected token), this time on the for. Updated the question description with this. Commented Apr 4, 2022 at 13:57
  • 2
    @AntoineWeber Just adding {} around the for loop wont work as the final content must be JSX. Commented Apr 4, 2022 at 13:59
  • Thanks for pointing out why the for loop does not work Commented Apr 4, 2022 at 14:05
  • This question is similar to: Loop inside React JSX. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 18 at 12:21

2 Answers 2

2

We must embed some JS code that returns some JSX or an array of JSX.

{new Array(5).fill().map((_) => (
      <TextField/>
      ))}

This code creates an array of 5 undefined elements. Then, .map returns an array with each undefined element transformed to <TextField/>.

Hence we return an array of JSX.

I believe the main reason it does not work is that the for loop does not return JSX.

Sign up to request clarification or add additional context in comments.

Comments

1

You cannot put javascript code in JSX declaration. You must use {} to denote any javascript code that is used to generate JSX.

Using something like this instead of your for loop would work as long as you put it in curly brackets.

 new Array(5).fill().map(() => <TextField/>)

Final result :

function App() {
    ...

    return (
        <Grid container className={classes.root}>
            <CssBaseline />
            <Grid item xs={false} md={7} className={classes.image} />
            <Grid item xs={12} md={5} component={Paper} elevation={6} square>
                <div className={classes.paper}>
                    <form className={classes.form} onSubmit={handleSubmit}>
                        {new Array(5).fill().map(() => <TextField/>)}
                        <Button
                            type="submit"
                            fullWidth
                            variant="contained"
                            color="primary"
                            className={classes.submit}
                        >
                            Submit
                        </Button>
                    </form>
                </div>
            </Grid>
        </Grid>
    );
}

3 Comments

Thanks. I am trying to understand why can't I use a regular for loop even with curly brackets?
@reddfox The curly brackets don't denote that you can write any javascript, but instead that you can use javascript functions to create JSX.
I think you forgot fill() in Array(5).fill().map

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.