React

by divyanshu013

HTML

<div id="app"></div>

<!-- Part-I: Create a React component that represents a circle. This circle changes its fill color between (red, green, blue) at an interval of 1s. -->

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

.done {
  color: rgba(0, 0, 0, 0.3);
  text-decoration: line-through;
}

input {
  margin-right: 5px;
}

React

class App extends React.Component {
	constructor(props) {
  	super(props);
    this.colors = {
  		0: "red",
      1: "green",
      2: "blue"
  	}
    this.state = {
    	color: this.colors[0],
      currentColor: 0
      
    }
  }
  changeColor()  {
    setTimeout(() => {
      if(this.state.currentColor === this.colors.length -1) {
        this.setState({
          color: this.colors[0],
          currentColor: 0
        })
      } else {
        this.setState({
          color: this.colors[this.state.currentColor + 1],
          currentColor: this.state.currentColor + 1
        })
        }
      }, 1000)
  }
  componentDidMount() {
  	this.changeColor()
  }
  componentWillUpdate() {
  	this.changeColor()
  }  
  render() {
    return (
      <div style={{
      	width: 100,
        height: 100,
        backgroundColor: this.state.color
      }}>
      <span>Color Changes</span>
      </div>
    )
  }
}

ReactDOM.render(<App />, document.querySelector("#app"))