0

I try to write an if statement inside the loop but it gives an error. I tried to use {} but it does not work too, how can I write it properly.

`<Option
      optionText={option}      

 colorText = {props.colors[index]}
      priceText = {props.prices[index]}
      rangeText = {props.ranges[index]}
      domainText = {props.domains[index]}

    if(this.props.colorText == this.props.domainText){
      //I want to write a if condition here but it says Identifier expected
    }
      count= {index + 1}
      handleDeleteOption={props.handleDeleteOption}
    />
    </div> 
      )) 
    }  
  </div>`

import React from 'react';

const Option = (props) => (

<p className="add-option-color">
  props.rangeText = {(props.colorText == props.domainText) ? props.rangeText : 
    props.domainText}
  {props.count}. Product: {props.optionText} , Color: {props.rangeText} , Price: {props.priceText}</p>


    <button  className="button button--link" onClick={(e) => {
        props.handleDeleteOption(props.optionText,props.colorText,props.priceText);
      }}
    >
      remove
    </button>
    <hr></hr>
  </div>
);

export default Option;
3
  • what is the prop you want to apply if the condition is true? Commented Nov 23, 2018 at 9:18
  • That if-statement is not inside a loop. It is inside a JSX element, and you can't write code inside a JSX element, unless it is as the properties value. Commented Nov 23, 2018 at 9:19
  • use ternary operator Commented Nov 23, 2018 at 9:20

2 Answers 2

2

The clean way is yo use the ternary operator like in this example :

<Option
      optionText={option}
      colorText = {props.colors[index]}
      priceText = {props.prices[index]}
      rangeText = {props.ranges[index]}
      domainText = {props.domains[index]}
      YOURPROPS = {(this.props.colorText == this.props.domainText) ? VALUEIFTRUE : 
      VALUEIFFALSE}
      count= {index + 1}
      handleDeleteOption={props.handleDeleteOption}
/>
Sign up to request clarification or add additional context in comments.

2 Comments

Why back tick??
It was a missclick, I removed it.
0

you can call a function like as following --->

getAllParams() {
        if(this.props.colorText == this.props.domainText){
          return "pass what you want pass"
        } else {
          return
        }
    }
    render() {    
    <Option
              optionText={option}      
              colorText = {props.colors[index]}
              priceText = {props.prices[index]}
              rangeText = {props.ranges[index]}
              domainText = {props.domains[index]}
              {...this.getParams()}
              count= {index + 1}
              handleDeleteOption={props.handleDeleteOption}
        />
    }

Comments

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.