0

So, this is what I'm doing inside of the return:

return (
    
    <React.Fragment>
      <Styles>
      <Container>
          <Row className="rows container">
          {results.length > 0 && (  
          {results.map(result => (
            
            <Col className="columns " xs={12} sm={6} md={4} lg={3} >
              <img src={result.poster_path}  alt="movie poster" />
              </Col>
              
          ))}
          )} 
              
          
        </Row>
        </Container>
        </Styles>
    </React.Fragment>
  )

But, it's giving me the error Unexpected token, expected "," . I don't understand, especially because I have used the same syntax in another component and that's not reporting any errors.

5
  • 2
    Which line do you get this error? What is the Unexpected token? Commented Jan 27, 2021 at 22:05
  • It's the results.map line Commented Jan 27, 2021 at 22:06
  • 2
    I recommend you post the entire component / a codesandbox.io to it if its too big, also I highly recommend the usage of Pretter (autoformat code) Commented Jan 27, 2021 at 22:07
  • 1
    remove the {} from around results.map Commented Jan 27, 2021 at 22:08
  • Thanks. this works. Commented Jan 27, 2021 at 22:14

1 Answer 1

2

Your return statement should look like this

return (
    <React.Fragment>
        <Styles>
            <Container>
                <Row className='rows container'>
                    {results.length > 0 &&
                        results.map(result => (
                            <Col className='columns ' xs={12} sm={6} md={4} lg={3}>
                                <img src={result.poster_path} alt='movie poster' />
                            </Col>
                        ))}
                </Row>
            </Container>
        </Styles>
    </React.Fragment>
)

(I removed the square brackets from around results.map)

Square brackets are used to inform jsx that you are going to execute javascript inside the jsx. You already started that execution on {results.length, if you add another pair of brackets inside the js execution block it will be perceived by js as you creating an object, hence the expected comma error.

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

1 Comment

oh okay. Actually, I was wrapping the curly braces around map in another component and it wasn't reporting any errors.. but before map I had bootstrap's Container and Row Components so maybe that's why it wasn't reporting any errors in that component. Anyway, your solution works for me. Thanks

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.