5:7 I got it right but I dont understand


#1

I just don't get it. The line with "var color = this.state.color == yellow ? green : yellow;", what the heck is going on here?
The line following I undertand that the state is changing but for some reason this exercise is not sitting right with me. Some please tell me how this entire code works. Well at least the Toggle Component.


https://www.codecademy.com/en/courses/react-101/lessons/this-state/exercises/call-setstate-another-function


var React = require('react');
var ReactDOM = require('react-dom');

var green = '#39D1B4';
var yellow = '#FFD712';

var Toggle = React.createClass({
  getInitialState: function(){
    return {color:green};
  },
  changeColor: function(){
    var color = this.state.color == yellow ? green : yellow;
    this.setState({color:color});
  },
  render: function () {
    return (
      <div style={{background: this.state.color}}>
        <h1>
          Change my color
        </h1>
        <button onClick={this.changeColor}>
          Change color
        </button>
      </div>
    );
  }
});
ReactDOM.render(
<Toggle />,
  document.getElementById('app')
);


#2

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