Home > database >  How to turn text in state to lowercase
How to turn text in state to lowercase

Time:10-27

I have text in the state and when I click on the div, I need to turn it into lowercase text. Now I'm statically setting the text and it's wrong. How can i do this?

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { text: "Hello" };

    this.toLowerCase = this.toLowerCase.bind(this);
  }

  toLowerCase() {
    this.setState({ text: "hello" });
  }
  render() {
    return <div onClick={this.toLowerCase}> {this.state.text} </div>;
  }
}

CodePudding user response:

use the state variable to update to lower case

this.setState({ text: this.state.text.toLowerCase() });
  • Related