简体   繁体   中英

Conditional rendering of component

I am trying to render a component only if asset.id === collection.masterAssetId . If this is the case a star icon should appear. I am using the getMasterId function to check this condition. Anyone that knows what the error is here?


Error:

Parsing error: Unexpected token

  70 |                     
  71 |                     {
> 72 |                       if(asset.id === this.getMasterId(asset.id)){
     |                       ^
  73 |                       <FaStar />
  74 |                     }}

App.js

import React from 'react';
import './App.css';
import {collections} from "./data.js"
import {assets} from "./data.js"
import {FontAwesome, FaStar} from "react-icons/fa"

class App extends React.Component {
    constructor() {
        super()

        this.state = {
           collectionsarr: collections,
           assetsarr: assets,
           clickedassets: []
        }
    }

    getMasterId(assetnr){
      const assetnum = this.state.collectionsarr.find(element => element.masterAssetId === assetnr).masterAssetId
      return assetnum
    }
  
  render(){
  return (
          <div className="App">
            <h1>Sitecore coding challenge</h1>
            
            <div className="left">
              {this.state.collectionsarr.map(element => 
                <div key={element.id}>
                  <p onClick={()=>this.handleAssetsClick(element.id)}>{element.name}</p>
                  <img src={this.getAssetPath(element.masterAssetId)} alt="pic"/>
                  <br></br>
                </div>
              )}
            </div>

            <div className="right">
                {this.state.clickedassets.map(asset => 
                  <div key={asset.id}>
                    <img src={require(`./${asset.path}`)} alt="pic"/>
                    <p>{asset.name}</p>
                    <p>{asset.id}</p>
                    <button onClick={() => this.makeMaster(asset.id)}>Make master!</button>
                    <p>icon "this is the master</p>
                    
                    {
                      if(asset.id === this.getMasterId(asset.id)){
                      <FaStar />
                    }}
                    
                    <br></br>
                  </div>
                )}
            </div>
          </div>
        )
  }
}

Syntax you're using is wrong, Change it to

 { asset.id === this.getMasterId(asset.id) && <FaStar /> }

Read more about conditional rendering here

You can't use statements in inline code within the return statement. You can only use expressions.

An if statement is a statement, so you should replace it with an expression such as the ternary operator x? y: z x? y: z or conditional and operator && :

// ternary
asset.id === this.getMasterId(asset.id) ? <FaStar /> : null

or:

// conditional and
asset.id === this.getMasterId(asset.id) && <FaStar />

React has some great docs on conditional rendering .

Be careful when using the conditional and && , as if the first argument is the number 0 (for example, list.length && <Component /> ) then you will end up rendering a 0 instead of nothing.

In those cases it's best to cast to a Boolean:

Boolean(list.length) && <Component />

or:

!!list.length && <Component />

See if this works

 (asset.id === this.getMasterId(asset.id))? <FaStar />: <></>;

Within the return code of the render function, you can only use expressions (things that are allowed on the right side of an equals). if is a statement, so it is not allowed. Example that doesn't work:

const a = if(asset.id === this.getMasterId(asset.id)) return <FaStar />

However, a ternary expression is allowed. This would then look like this:

const a = asset.id === this.getMasterId(asset.id) ? <FaStar /> : null

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