0
<div className="col-md-9">
           <span style={{color:this.props.todo.done ?'#e6e6e6' : '#4d4d4d'},
                        {textDecoration: this.props.todo.done ? '#e6e6e6 line-through' : 'none'}
                    }
           >
               {this.props.todo.value}
           </span>
</div>

I know the above one is wrong, but is there anyway to add multiple style attributes inside the span tag and make it work. As you can see I want both of them to work. Thank you

6
  • refer codeburst.io/4-four-ways-to-style-react-components-ac6f323da822 Commented Aug 31, 2018 at 6:06
  • You can certainly add multiple styles - what error is your react outputting from your code? Commented Aug 31, 2018 at 6:07
  • yeah I saw this before posting, but I didn't get a clear idea on implementing the above problem.. anyways thank you for your help Commented Aug 31, 2018 at 6:08
  • @ChrisCousins it is not showing any errors, the declaration which is given at last is working not the ones before that.. Commented Aug 31, 2018 at 6:09
  • Oh I see, you should only supply one object to the style prop and in it you can have multiple style key value pairs - you’re sending two separate style objects. Commented Aug 31, 2018 at 6:11

2 Answers 2

1

Still you can optimize code like this, so that your render method looks neat.

Create variable of style JSON object.

const customStyle = {
  color: this.props.todo.done ? "#e6e6e6" : "#4d4d4d",
  textDecoration: this.props.todo.done ? "#e6e6e6 line-through" : "none"
};

Pass customStyle object as props to style element.

<div className="col-md-9">
   <span style={customStyle}>{this.props.todo.value}</span>
</div>;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, definitely I will give it a try :)
1

try this, i just removed redundant curly drackets

<div className="col-md-9">
       <span style={
                     {
                       color:this.props.todo.done ?'#e6e6e6' : '#4d4d4d',
                       textDecoration: this.props.todo.done ? '#e6e6e6 line-through' : 'none'
                     }
                }
       >
           {this.props.todo.value}
       </span>
</div>

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.