Example1
import React, { useState } from "react";
function App() {
const [headingText, setHeadingText] = useState("Hello");
const [isMousedOver, setMouseOver] = useState(false);
function handleClick() {
setHeadingText("Submitted");
}
function handleMouseOver() {
setMouseOver(true);
}
function handleMouseOut() {
setMouseOver(false);
}
return (
<div className="container">
<h1>{headingText}</h1>
<input type="text" placeholder="What's your name?" />
<button
style={{ backgroundColor: isMousedOver ? "black" : "white" }}
onClick={handleClick}
onMouseOver={handleMouseOver}
onMouseOut={handleMouseOut}
>
Submit
</button>
</div>
);
}
export default App;
Example2
import React, { useState } from "react";
function App() {
const [name, setName] = useState("");
const [headingText, setHeading] = useState("");
function handleChange(event) {
console.log(event.target.value);
setName(event.target.value);
}
function handleClick(event) {
setHeading(name);
event.preventDefault();
}
return (
<div className="container">
<h1>Hello {headingText}</h1>
<form onSubmit={handleClick}>
<input
onChange={handleChange}
type="text"
placeholder="What's your name?"
value={name}
/>
<button type="submit">Submit</button>
</form>
</div>
);
}
export default App;
'프로그래밍 > Web' 카테고리의 다른 글
Changing Complex State - What to Do When You have Functions that Do the Same Thing (0) | 2024.05.10 |
---|---|
Difference between Class and Functional Component (0) | 2024.05.09 |
Javascript ES6 Object & Array Destructuring (0) | 2024.05.07 |
React Hook - useState (0) | 2024.05.06 |
React Conditional Rendering with the Tenary Operator % AND Operator (0) | 2024.05.03 |