App.jsx
import React from "react";
import ClassComponent from "./ClassComponent";
import FunctionalComponent from "./FunctionalComponent";
// class App extends React.Component {
// render() {
// return <ClassComponent />;
// }
// }
function App() {
return <FunctionalComponent />;
}
export default App;
ClassComponent.jsx
import React from "react";
class ClassComponent extends React.Component {
constructor() {
super();
this.state = {
count: 0
};
this.increase = this.increase.bind(this);
}
increase() {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<div>
<h1>{this.state.count}</h1>
<button onClick={this.increase}>+</button>
</div>
);
}
}
export default ClassComponent;
FunctionalComponent.jsx
import React, { useState } from "react";
function FunctionalComponent() {
const [count, setCount] = useState(0);
function increase() {
setCount(count + 1);
}
return (
<div>
<h1>{count}</h1>
<button onClick={increase}>+</button>
</div>
);
}
export default FunctionalComponent;
'프로그래밍 > Web' 카테고리의 다른 글
ES6 Spread Operator - How to Simplify Your Code (0) | 2024.05.13 |
---|---|
Changing Complex State - What to Do When You have Functions that Do the Same Thing (0) | 2024.05.10 |
Event Handling in React - How to Handle React Forms (0) | 2024.05.08 |
Javascript ES6 Object & Array Destructuring (0) | 2024.05.07 |
React Hook - useState (0) | 2024.05.06 |