简体   繁体   中英

React typescript Parsing error: '{' expected

i'm using react + typescript and get this error. Parsing error: '{' expected how to fix it?.

here is my code snippet

  render() {
    console.log(this.state.collection);
    return (
      <div className='body-container'>
        <Container>
          {this.collection.items.map(collection => {
              <Card>
                <Card.Image>
                  <Image.Container size='3by4'>
                    <Image src=collection.coverLink />
                  </Image.Container>
                </Card.Image>
              </Card>
            });
          }
        </Container>
      </div>
    );
  }

Parsing error: '{' expected

You need to wrap the attribute in {

  render() {
    console.log(this.state.collection);
    return (
      <div className='body-container'>
        <Container>
          {this.collection.items.map(collection => {
            <Card>
              <Card.Image>
                <Image.Container size='3by4'>
                  <Image src={collection.coverLink} /> {/*fix here*/}
                </Image.Container>
              </Card.Image>
            </Card>
          })} {/*another fix here*/}
      </Container>
      </div>
    );
  }

Also removed the semicolon ;).

Please remove ;

  render() {
    console.log(this.state.collection);
    return (
      <div className='body-container'>
        <Container>
          {this.collection.items.map(collection => {
              <Card>
                <Card.Image>
                  <Image.Container size='3by4'>
                    <Image src=collection.coverLink />
                  </Image.Container>
                </Card.Image>
              </Card>
            }); // Remove ;
          }
        </Container>
      </div>
    );
  }

Try to change {} by ()

render() {
    console.log(this.state.collection);
    return (
      <div className='body-container'>
        <Container>
          {this.collection.items.map(collection => (
              <Card>
                <Card.Image>
                  <Image.Container size='3by4'>
                    <Image src=collection.coverLink />
                  </Image.Container>
                </Card.Image>
              </Card>
            ))
          }
        </Container>
      </div>
    );
  }

Or make Card inside return()

render() {
    console.log(this.state.collection);
    return (
      <div className='body-container'>
        <Container>
          {this.collection.items.map(collection => {
              return(
              <Card>
                <Card.Image>
                  <Image.Container size='3by4'>
                    <Image src=collection.coverLink />
                  </Image.Container>
                </Card.Image>
              </Card>
              )
            })
          }
        </Container>
      </div>
    );
  }

Firstly, you need to add {} around the prop to Image component, like this: <Image src={collection.coverLink} /> .

Sencondy, you are not returning anything from the function in map.

WRONG:

{this.collection.items.map(collection => {
    <Card>
      <Card.Image>
        <Image.Container size='3by4'>
          <Image src={collection.coverLink} />
        </Image.Container>
      </Card.Image>
    </Card>
  });
}

GOOD:

{this.collection.items.map(collection => (
    <Card>
      <Card.Image>
        <Image.Container size='3by4'>
          <Image src={collection.coverLink} />
        </Image.Container>
      </Card.Image>
    </Card>
  ));
}

OR:

{this.collection.items.map(collection => {
    return (
      <Card>
        <Card.Image>
          <Image.Container size='3by4'>
            <Image src={collection.coverLink} />
          </Image.Container>
        </Card.Image>
      </Card>
    );
  });
}

You missed flower brackets , add the following

Use it to wrap the value of "src" attribute

 <Image src={collection.coverLink} />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM