Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

javascript - Getting unexpected token, expected "," while trying to map an array in React

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.

question from:https://stackoverflow.com/questions/65928074/getting-unexpected-token-expected-while-trying-to-map-an-array-in-react

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...