React - Change Inline CSS Conditionally Based on Component State

Tell us what’s happening:
Someone can help me please with this challenge ? I’m trying to do it with normal javascript ‘if’, but it seems like I don’t right well the code to make the border = 3px solid red. What did I do wrong ? Thank you

  **Your code so far**
class GateKeeper extends React.Component {
constructor(props) {
  super(props);
  this.state = {
    input: ''
  };
  this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
  this.setState({ input: event.target.value })
}
render() {
  let inputStyle = {
    border: '1px solid black'
  };
  // Change code below this line
if(this.state.input.length > 15) {
return border= '3px solid red'
}
  // Change code above this line
  return (
    <div>
      <h3>Don't Type Too Much:</h3>
      <input
        type="text"
        style={inputStyle}
        value={this.state.input}
        onChange={this.handleChange} />
    </div>
  );
}
};
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15

Challenge: React - Change Inline CSS Conditionally Based on Component State

Link to the challenge:

You should type inputStyle.border instead of border, because it would be undefined otherwise

[Removed by moderators]

If you return it, it will pass ‘3px solid red’ as output if you exceed 15 character

1 Like

Thanks you, I just passed the challenge thanks to your help. Is there any way to do it with ternary operator ??? I tried this but it doesn’t work of course =>this.state.input.length > 15 ? inputStyle.border= 3px solid red

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Your ternary operator expression misses a component, and here is a hint:
The syntax of the ternary operator is as follows:

condition? x:y

With x the expression is executed if the condition is True, and y if the condition is False.

I have a big problem to figure out the y part. still does not work
this.state.input.length > 15 ? inputStyle.border= ‘3px solid red’: input

The y is inputStyle.border, which refers to the original state (1px solid black)

1 Like

Thanks ! Now I understand. So the y is = inputStyle.border = “1px solid black”

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.