๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

React

[ React / Select ] Select์˜ defaultValue ๋ฅผ state๋ฅผ ์ด์šฉํ•˜์—ฌ ๋™์  ๋ณ€๊ฒฝ ์‹œํ‚ค๊ธฐ.

 

โ—๏ธ ์ด์Šˆ ๋ฐœ์ƒ

React ํ”„๋กœ์ ํŠธ๋ฅผ ์ž‘์—… ์ค‘์— ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ state๋กœ ๊ด€๋ฆฌํ•ด์„œ ํ•„ํ„ฐ๋ง ์˜ต์…˜์„ ์„ ํƒํ•  ๋•Œ๋งˆ๋‹ค ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋™์  ํ•„ํ„ฐ๋ง ๊ตฌ์กฐ๋กœ ์ž‘์—… ์ค‘์— ์ผ๋ฐ˜ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋“ค์€ ๋™์  ํ•„ํ„ฐ๋ง ๊ธฐ๋Šฅ์ด ์ž˜ ์ ์šฉ๋˜๋Š”๋ฐ, Select์˜  defaultValue๋Š” ์ด์ „ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๊ฐ–๊ณ  ๋™์  ํ•„ํ„ฐ๋ง์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š” ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค. 

 

 

 

์˜ˆ์‹œ ์ฝ”๋“œ

import React, {useState, useEffect} from 'react';
// UI Library๋Š” Antdesign์„ ์‚ฌ์šฉ.
import { Select } from 'antd';
const { Option } = Select;


const data = [
 {name: "Lee", isStudent: false},
 {name: "Kim", isStudent: true},
 {name: "Park", isStudent: true},
 {name: "Oh", isStudent: false}
];


const MainPage = () => {
  const [listData, setListData] = useState([]); // data state
  
  useEffect({
  	setListData(data); // ์ตœ์ดˆ 1ํšŒ data๋ฅผ listData state์— ๋„ฃ์–ด์ค€๋‹ค.
  } ,[])
 
 
    const columns = [ // table์—์„œ ์‚ฌ์šฉํ•  columns
        {
          title: "์ด๋ฆ„",
          dataIndex: "name",
          align: "center",
          width: "80%",
          key: "name",
        },
        {
          title: "๋‚˜์ด",
          dataIndex: "age",
          align: "center",
          width: "20%",
          key: "age",
          render: (value) => (
               <Select
                defaultValue={value}
                onChange={onChange}
              >
                <Option value={true}>ํ•™์ƒ</Option>
                <Option value={false}>์ผ๋ฐ˜์ธ</Option>
              </Select>
            );
        },
    ];
  
  
  function onChangeOption(value){ // Radio ์˜ต์…˜์„ ๋ณ€๊ฒฝํ• ๋•Œ๋งˆ๋‹ค ๋ฐœ์ƒํ•  ํ•„ํ„ฐ๋ง ์ด๋ฒคํŠธ. 
     switch (value) {
      case "all":
        return setListData(data);
        break;
      case "student":
        const filterStudents = data.filter((item) => {
          return item.isStudent === true;
        });
        return setListData(filterStudents);
        break;
      case "ordinary":
        const ordinaryPerson = data.filter((item) => {
          return item.isStudent === false;
        });
        return setListData(ordinaryPerson);
        break;
        };
    };
  
  
  return (
  	<div>
    	<Radio.Group
          defaultValue="all"
          onChange={(value) => {
            onChangeOption(value.target.value);
          }}
        >
          <Radio value="all">์ „์ฒด</Radio>
          <Radio value="student">ํ•™์ƒ</Radio>
          <Radio value="ordinary">์ผ๋ฐ˜์ธ</Radio>
        </Radio.Group>
        <Table
            dataSource={listData}
            columns={columns}
          />
    </div>
  )
};

export default MainPage;

 

 

๋Œ€๋žต ์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ๋ผ๋””์˜ค ๋ฒ„ํŠผ์œผ๋กœ ์˜ต์…˜์„ ์„ ํƒํ•˜๋ฉด, ์›๋ณธ ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•„ํ„ฐ๋งํ•˜์—ฌ  ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ state์— ํ•„ํ„ฐ๋ง๋œ ๊ฒฐ๊ณผ๋ฌผ์„ ๋„ฃ์–ด์ฃผ๊ณ , Table์—์„œ๋Š” ํ•„ํ„ฐ๋ง๋œ ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ state๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ตฌ์กฐ๋กœ ๋งŒ๋“ค์–ด์ ธ ์žˆ์—ˆ๋‹ค.

 

 

ํ•™์ƒ๊ณผ ์ผ๋ฐ˜์ธ์„ ๊ตฌ๋ถ„ํ•˜๋Š” Radio ๋ฒ„ํŠผ.

 

Radio๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ „์ฒด / ํ•™์ƒ / ์ผ๋ฐ˜์ธ์ด ํ•„ํ„ฐ๋ง ๋œ๋‹ค.

 

 

 

๊ทธ๋Ÿฐ๋ฐ ํ…Œ์ด๋ธ”์˜ "์ด๋ฆ„"(๋‹จ์ˆœ ํ…์ŠคํŠธ ํ˜•ํƒœ)์˜ ๊ฒฝ์šฐ, ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋™์ ์œผ๋กœ ํ•„ํ„ฐ๋ง๋˜๋ฉฐ ์ž˜ ๊ตฌํ˜„๋˜์—ˆ์ง€๋งŒ, "๋‚˜์ด"(select์˜ default value๋กœ ๊ธฐ๋ณธ๊ฐ’์ด ๋…ธ์ถœ๋จ)์˜ ๊ฒฝ์šฐ๋Š” defaultValue๊ฐ€ ์ด์ „ ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ state์˜ ๊ฐ’์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์—ˆ๋‹ค.

 

์ด์œ ๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, default value๋ฅผ state๋กœ ๋ฐ›๊ฒŒ ํ•ด๋„, ์ฝ˜์†”์—๋Š” ๋ถ„๋ช…ํžˆ ์ƒˆ๋กœ์šด state๋ฐ์ดํ„ฐ๊ฐ€ ์ฐํžˆ๋Š”๋ฐ ui์—์„œ๋Š” ์—ฌ์ „ํžˆ ์ด์ „ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚˜์˜ค๊ณ  ์žˆ์—ˆ๋‹ค. state์— ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€ํ•˜์—ฌ useEffect๋กœ ๋„ฃ์–ด์ฃผ์–ด๋„ ๋งˆ์ฐฌ๊ฐ€์ง€.. 

 

๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ Select์˜ default value๊ฐ€ ์•„๋‹Œ value๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ์—์„œ ๋ฐ›์•„์˜ค๊ณ  Select์˜ onChange ์ด๋ฒคํŠธ๋ฅผ ํ†ตํ•ด value ๊ฐ’์„ ๋™์ ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.

 

 

 

 


 

 

 

 

ํ•ด๊ฒฐ ์˜ˆ์‹œ ์ฝ”๋“œ

import React, {useState, useEffect} from 'react';
// UI Library๋Š” Antdesign์„ ์‚ฌ์šฉ.
import { Select } from 'antd';
const { Option } = Select;


const data = [
 {name: "Lee", isStudent: false},
 {name: "Kim", isStudent: true},
 {name: "Park", isStudent: true},
 {name: "Oh", isStudent: false}
];


const MainPage = () => {
  const [listData, setListData] = useState([]); // data state
  const [ selectValue, setSelectValue] = useState('');
  
  useEffect({
  	setListData(data); // ์ตœ์ดˆ 1ํšŒ data๋ฅผ listData state์— ๋„ฃ์–ด์ค€๋‹ค.
  } ,[])
 
 
    const columns = [ // table์—์„œ ์‚ฌ์šฉํ•  columns
        {
          title: "์ด๋ฆ„",
          dataIndex: "name",
          align: "center",
          width: "80%",
          key: "name",
        },
        {
          title: "๋‚˜์ด",
          dataIndex: "age",
          align: "center",
          width: "20%",
          key: "age",
          render: (item) => {
          	if(value === ''){
            	setSelectValue(item);
            }
               return <Select
                value={selectValue}
                onChange={(v) => { 
                	setSelectValue(v.tartget.value);
                }}
              >
                <Option value={true}>ํ•™์ƒ</Option>
                <Option value={false}>์ผ๋ฐ˜์ธ</Option>
              </Select>
            };
        },
    ];
  
  
  function onChangeOption(value){ // Radio ์˜ต์…˜์„ ๋ณ€๊ฒฝํ• ๋•Œ๋งˆ๋‹ค ๋ฐœ์ƒํ•  ํ•„ํ„ฐ๋ง ์ด๋ฒคํŠธ. 
     switch (value) {
      case "all":
        return setListData(data);
        break;
      case "student":
        const filterStudents = data.filter((item) => {
          return item.isStudent === true;
        });
        return setListData(filterStudents);
        break;
      case "ordinary":
        const ordinaryPerson = data.filter((item) => {
          return item.isStudent === false;
        });
        return setListData(ordinaryPerson);
        break;
        };
    };
  
  
  return (
  	<div>
    	<Radio.Group
          defaultValue="all"
          onChange={(value) => {
            onChangeOption(value.target.value);
          }}
        >
          <Radio value="all">์ „์ฒด</Radio>
          <Radio value="student">ํ•™์ƒ</Radio>
          <Radio value="ordinary">์ผ๋ฐ˜์ธ</Radio>
        </Radio.Group>
        <Table
            dataSource={listData}
            columns={columns}
          />
    </div>
  )
};

export default MainPage;

 

 

์ด๋Ÿฐ ์‹์œผ๋กœ select์˜ default value๊ฐ€ ์•„๋‹Œ value๋กœ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•ด์ฃผ๋ฉด option์œผ๋กœ value๊ฐ’์„ ๋ฐ”๊ฟ”์ฃผ์–ด๋„ state์˜ select value๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ž˜์„œ selectValue state๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์–ด, selectValue state ๊ฐ’์ด ์ดˆ๊ธฐ๊ฐ’ ์ผ ๋•Œ๋งŒ default value๋กœ ๋ฐ์ดํ„ฐ์—์„œ ๋ฐ›์•„์˜จ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š” if๋ฌธ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ , option์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋งˆ๋‹ค setSelectValue ์ด๋ฒคํŠธ๋ฅผ ํ†ตํ•ด ๋ณ€๊ฒฝ๋œ selectValue๊ฐ’์„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

 

์™œ select์˜ default value๊ฐ€ ๋™์ ์œผ๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”์ง€ ๋” ์ฐพ์•„๋ณด๊ณ  ์ •๋ฆฌํ•˜์—ฌ ์ถ”๊ฐ€ํ•ด์•ผ๊ฒ ๋‹ค.