DEV Community

adriangheo
adriangheo

Posted on

02.05 React State - font color switch (class components)

App preview:
The way the app will look like

Project files:
An image of project file structure


src/App.js:

import React from "react";
import "./App.css"

class App extends React.Component{
  state = {
    color: 'blue',
  };

  turnFontRed(){
    this.setState({ color: 'red'});
  }

  turnFontBlue(){
    this.setState({ color: 'blue'});
  }

  render(){
    return(
      <div className="app" style={{color: this.state.color}}>
        <h1>Text color is {this.state.color}.</h1>
        <button onClick={()=>{this.turnFontRed()}}>Turn font red</button>
        <button onClick={()=>{this.turnFontBlue()}}>Turn font blue</button>
      </div>
    )
  }
}

export default App;
Enter fullscreen mode Exit fullscreen mode

src/App.css

.app{
    background-color: lightskyblue;
    padding: 20px 10px;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)