بتل کد Battle Codeدارد باز می‌شود…

مثال JavaScript: <script type="text/jsx"> — بررسی ورودی 2 3

مثال 4 از درس جلسه‌ی 19 آموزش ری‌اکت (React) از صفر برای نوجوانان، قطعه به قطعه: بررسی ورودی

بعضی بررسی‌ها را می‌شود همان لحظه‌ی تایپ نشان داد و state تازه هم نمی‌خواهند. چون متن کادر در state است، در هر بار کشیدن صفحه می‌توانی از روی آن یک true یا false بسازی و با آن پیام یا کلاس را انتخاب کنی.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.good { color: green; }
.bad { color: crimson; }
</style>

<div id="root" dir="ltr"></div>

<script type="text/jsx">
import { useState } from "react";
import { createRoot } from "react-dom/client";

function App() {
  const [pass, setPass] = useState("");
  const strong = pass.length >= 6;
  return (
    <div>
      <input value={pass} onChange={(event) => setPass(event.target.value)} />
      <p className={strong ? "good" : "bad"}>{strong ? "Strong" : "Too short"}</p>
    </div>
  );
}

createRoot(document.getElementById("root")).render(<App />);
</script>

این کد را همین‌جا در ویرایشگر HTML اجرا کن، عددها و متن‌هایش را عوض کن و دوباره اجرا کن تا ببینی چه تغییری می‌کند.

بیشتر بخوان: جاوااسکریپت چیست؟ از دکمه‌ی وب تا Node.js

← مثال قبلی · برگشت به درس جلسه‌ی 19: بررسی ورودی · مثال بعدی → · چالش‌های این جلسه