# freeCodeCamp Challenge Guide: Set State with this.setState

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-set-state-with-this-setstate/301412>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 12:57pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-set-state-with-this-setstate/301412 "2019-08-03T12:57:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 12:57pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-set-state-with-this-setstate/301412/1 "2019-08-03T12:57:51Z")

</div>

# [Set State with this.setState](https://www.freecodecamp.org/learn/front-end-libraries/react/set-state-with-this-setstate)

* * *

## Hints

### Hint 1

```JSX
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: 'Initial State'
    };
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick() {
    // change code below this line
       // Update the state data by using "this.setState()" method.
       // You can look to the sample inside the description for calling "setState()" method.
    // change code above this line
  }
  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Click Me</button>
        <h1>{this.state.name}</h1>
      </div>
    );
  }
};

```

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```JSX
> class MyComponent extends React.Component {
> constructor(props) {
> super(props);
> this.state = {
> name: 'Initial State'
> };
> this.handleClick = this.handleClick.bind(this);
> }
> handleClick() {
> // change code below this line
> this.setState({
> name: 'React Rocks!'
> });
> // change code above this line
> }
> render() {
> return (
> <div>
> <button onClick={this.handleClick}>Click Me</button>
> <h1>{this.state.name}</h1>
> </div>
> );
> }
> };
> 
> ```
> 
> #### Code Explanation
> 
> - When users click the button the “handleClick()” method will be called and  
> inside this method the data of the constuctor`s state will be updated by “setState()” method.  
> then h1 tag will be changed with the new data of the constructor`s state.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:58pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-set-state-with-this-setstate/301412/2 "2025-01-08T15:58:18Z")

</div>

Closed on January 8, 2025, 3:58pm UTC


