-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-2.js
More file actions
34 lines (34 loc) · 878 Bytes
/
react-2.js
File metadata and controls
34 lines (34 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
visibility: false
};
// change code below this line
this.toggleVisibility = this.toggleVisibility.bind(this);
// change code above this line
}
// change code below this line
toggleVisibility(){
this.setState({
visibility: !this.state.visibility
});
}
// change code above this line
render() {
if (this.state.visibility) {
return (
<div>
<button onClick={this.toggleVisibility}>Click Me</button>
<h1>Now you see me!</h1>
</div>
);
} else {
return (
<div>
<button onClick={this.toggleVisibility}>Click Me</button>
</div>
);
}
}
};//https://learn.freecodecamp.org/front-end-libraries/react/use-state-to-toggle-an-element