๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ Cross Platform/React Native

[React Native] Form Component ๋งŒ๋“ค์–ด Screen์— ์ ์šฉํ•ด ๋ณด๊ธฐ(Apply Form Component to Screen)

by Fomagran ๐Ÿ’ป 2022. 6. 22.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

React Native์—์„œ Form์„ ๋งŒ๋“ค์–ด ํ•ด๋‹น Screen์— ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~!

 

์–ธ์–ด๋Š” TypeScript, ํ™˜๊ฒฝ์€ Expo๋กœ ์ง„ํ–‰ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!


AddPersonForm.tsx

 

๋จผ์ € ์‚ฌ๋žŒ์„ ์ถ”๊ฐ€ํ•˜๋Š” Form์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

PersonFormType

 

Form์— ๋“ค์–ด๊ฐˆ ํ”„๋กœํผํ‹ฐ๋“ค์„ ์ •์˜ํ•ด ์ค๋‹ˆ๋‹ค.

 

export type PersonForm = {
  name: string;
  age: string;
  gender: string;
};

 

PersonProps

 

Form์˜ ํ”„๋กœํผํ‹ฐ์˜ type์„ ์ •์˜ํ•ด ์ค๋‹ˆ๋‹ค.

 

์ œ์ถœ ํ•˜์˜€์„ ๋•Œ PersonForm์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

type PersonFormProps = {
  onSubmit: (form: PersonForm) => void;
};

 

AddPersonForm

 

AddPersonForm์— ์œ„์—์„œ ์ •์˜ํ•œ PersonFormProps๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ž‘์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

export function AddPersonForm({ onSubmit }: PersonFormProps) { ...

 

formState

 

form์˜ ์ƒํƒœ๋ฅผ ์ €์žฅํ•  useState๋ฅผ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

  const [form, setForm] = useState({
    name: "",
    age: "",
    gender: "",
  });

 

View

 

TextInput์„ ์ด์šฉํ•˜์—ฌ ๊ฐ ํ”„๋กœํผํ‹ฐ์— ๋งž๋Š” Input ์ฐฝ์„ ๋งŒ๋“ค์–ด ์ฃผ๊ณ ,

 

์•„๋ž˜์— ์ œ์ถœ์„ ํ•  ๋ฒ„ํŠผ์ธ Submit์„ ๊ตฌํ˜„ํ•ด ์ฃผ๊ณ , ํ•ด๋‹น ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ form ์ •๋ณด๋ฅผ ๋‹ด์•„ onSubmit์ด ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

  return (
    <View>
      <TextInput
        placeholder="name"
        style={styles.input}
        onChangeText={onChangeText("name")}
        value={form.name}
      />
      <TextInput
        placeholder="age"
        style={styles.input}
        onChangeText={onChangeText("age")}
        value={form.age}
      />
      <TextInput
        placeholder="gender"
        style={styles.input}
        onChangeText={onChangeText("gender")}
        value={form.gender}
      />
      <Button onPress={() => onSubmit(form)} title="Submit"></Button>
    </View>
  );

 

onChangeText

 

ํ…์ŠคํŠธ๊ฐ€ ์ˆ˜์ •๋˜์—ˆ์„ ๋•Œ ์–ด๋–ค ์ธํ’‹์ฐฝ์ด ์ˆ˜์ •๋œ ๊ฑด์ง€ prop์œผ๋กœ ์•Œ์•„๋‚ธ ๋’ค form์˜ ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ์— value๊ฐ’์„ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค.

 

  const onChangeText = (prop: string) => (value: string) => {
    setForm({
      ...form,
      [prop]: value,
    });
  };

AddPersonScreen.tsx

 

์ œ์ถœํ–ˆ์„ ๋•Œ form ์ •๋ณด๋ฅผ alert๋กœ ๋„์šธ handleSumit ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

View์—” ์œ„์—์„œ ๋งŒ๋“  AddPersonForm ๋„ฃ์–ด์ฃผ๊ณ  onSubmit์ด ๋˜์—ˆ์„ ๋•Œ handleSubmit์ด ์‹คํ–‰๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

export default function AddPersonScreen() {
  const handleSubmit = (form: PersonForm) => {
    alert(`Name: ${form.name} Age: ${form.age} Gender: ${form.gender}`);
  };
  
  return (
    <View style={styles.container}>
      <AddPersonForm onSubmit={handleSubmit}></AddPersonForm>
    </View>
  );
}

์‹คํ–‰ ํ™”๋ฉด

 

ํ•ด๋‹น ์ธํ’‹์ฐฝ์— ๊ฐ ๊ฐ’์„ ์ž…๋ ฅํ•˜๊ณ  Submit ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด alert ์ฐฝ์— ํ•ด๋‹น form ์ •๋ณด๊ฐ€ ๋„์›Œ์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€