Find the Bugs چالش 6 از جلسهی 18، سخت
این فرم باید رنگهای تازه را به فهرست اضافه کند، ولی دو اشتباه دارد:
- با ثبت فرم صفحه از نو باز میشود.
- اگر آن را درست کنی، میبینی رنگ تازه بهجای اضافه شدن، جای همهی رنگهای قبلی را میگیرد.
هر دو را در تابع add درست کن.
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
</style>
<div id="root" dir="ltr"></div>
<script type="text/jsx">
import { useState } from "react";
import { createRoot } from "react-dom/client";
function App() {
const [text, setText] = useState("");
const [colors, setColors] = useState(["red", "blue"]);
function add(event) {
event.preventDefault;
setColors([text]);
setText("");
}
return (
<div>
<form id="form" onSubmit={add}>
<input id="color" value={text} onChange={(event) => setText(event.target.value)} />
<button id="add">Add</button>
</form>
<ul id="list">
{colors.map((color, i) => <li key={i}>{color}</li>)}
</ul>
</div>
);
}
createRoot(document.getElementById("root")).render(<App />);
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 18: فرمها
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js