<!DOCTYPE html>
<html>
<body>
<div id="root"></div>
</body>
<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
const root = document.getElementById("root");
let counter = 0;
function countUp() {
counter = counter + 1;
render()
}
function render() {
ReactDOM.render(<App />, root);
}
const App = () => (
<div>
<h3>
Total Clicks: {counter}
</h3>
<button onClick={countUp}>
Click me
</button>
</div>
);
render();
</script>
</html>
let counter = 0;
function countUp() {
}
와
const data = React.useState(0);
는 같은 역할을 함 [0,f] 라는 배열 반환
function App() {
const [counter, modifier] = React.useState(0);
return (
<div>
<h3>Total Clicks: {counter}</h3>
<button>Click me</button>
</div>
)
};
function App() {
const [counter, setCounter] = React.useState(0)
const onClick = () => {
setCounter(counter + 1);
};
return (
<div>
<h3>Total Clicks: {counter}</h3>
<button onClick={onClick}>Click me</button>
</div>
)
};
<!DOCTYPE html>
<html>
<body>
<div id="root"></div>
</body>
<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
const root = document.getElementById("root");
function App() {
const [minutes, setMinutes] = React.useState()
const onChange = () => {
setMinutes(event.target.value);
}
const reset = () => {
setMinutes(0);
}
return (
<div>
<div>
<h1 className="hi">Super converter</h1>
<label htmlFor="minutes">Minutes</label>
<input
id="minutes"
value={minutes}
placeholder="Minutes"
type="number"
onChange={onChange}
/>
<h4>You want to convert {minutes}</h4>
<label htmlFor="hours">Hours</label>
<input
id="hours"
value={Math.round(minutes / 60)}
placeholder="Hours"
type="number"
/>
</div>
<button onClick={reset}>Reset</button>
</div>
)
};
ReactDOM.render(<App />, root);
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="root"></div>
</body>
<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
const root = document.getElementById("root");
function App() {
const [amount, setAmount] = React.useState()
const [inverted, setInverted] = React.useState(false)
const onChange = () => {
setAmount(event.target.value);
}
const reset = () => {
setAmount(0);
}
const onFlip = () => {
reset();
setInverted((current) => !current);
}
return (
<div>
<div>
<h1 className="hi">Super converter</h1>
<label htmlFor="minutes">Minutes</label>
<input
id="amount"
value={inverted ? amount * 60 : amount}
placeholder="Minutes"
type="number"
onChange={onChange}
disabled={inverted}
/>
<h4>You want to convert {amount}</h4>
<label htmlFor="hours">Hours</label>
<input
id="hours"
value={inverted ? amount : Math.round(amount / 60)}
placeholder="Hours"
type="number"
onChange={onChange}
disabled={!inverted}
/>
</div>
<button onClick={reset}>Reset</button>
<button onClick={onFlip}>{inverted ? "Turn back" : "Invert"}</button>
</div>
)
};
ReactDOM.render(<App />, root);
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="root"></div>
</body>
<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
const root = document.getElementById("root");
function MinutesToHours() {
const [amount, setAmount] = React.useState()
const [inverted, setInverted] = React.useState(false)
const onChange = () => {
setAmount(event.target.value);
}
const reset = () => {
setAmount(0);
}
const onFlip = () => {
reset();
setInverted((current) => !current);
}
return (
<div>
<div>
<h3>Min to Hour</h3>
<label htmlFor="minutes">Minutes</label>
<input
id="amount"
value={inverted ? amount * 60 : amount}
placeholder="Minutes"
type="number"
onChange={onChange}
disabled={inverted}
/>
<label htmlFor="hours">Hours</label>
<input
id="hours"
value={inverted ? amount : Math.round(amount / 60)}
placeholder="Hours"
type="number"
onChange={onChange}
disabled={!inverted}
/>
</div>
<button onClick={reset}>Reset</button>
<button onClick={onFlip}>{inverted ? "Turn back" : "Invert"}</button>
</div>
)
};
function KmToMiles() {
const [kmAmount, setKmAmount] = React.useState()
const [inverted, setInverted] = React.useState(false)
const onChange = () => {
setKmAmount(event.target.value);
}
const reset = () => {
setKmAmount(0);
}
const onFlip = () => {
reset();
setInverted((current) => !current);
}
return <div>
<div>
<h3>KM to M</h3>
<label htmlFor="km">Km</label>
<input
id="kmAmount"
value={inverted ? kmAmount * 1.6 : kmAmount}
placeholder="km"
type="number"
onChange={onChange}
disabled={inverted}
/>
<label htmlFor="miles">Miles</label>
<input
id="miles"
value={inverted ? kmAmount : kmAmount * 0.62}
placeholder="Miles"
type="number"
onChange={onChange}
disabled={!inverted}
/>
</div>
<button onClick={reset}>Reset</button>
<button onClick={onFlip}>{inverted ? "Turn back" : "Invert"}</button>
</div>
}
function App() {
const[index, setIndex] = React.useState("0")
const onSelect = (event) =>{
setIndex(event.target.value)
}
return (
<div>
<h1>Super Converter</h1>
<select value={index} onChange={onSelect}>
<option value="xx">Select your units</option>
<option value="0">Minutes & Hours</option>
<option value="1">Km & Miles</option>
</select>
<hr />
{index === "xx" ? "Please select your unit" : null}
{index === "0" ? <MinutesToHours /> : null}
{index === "1" ? <KmToMiles /> : null}
</div>
);
};
ReactDOM.render(<App />, root);
</script>
</html>