๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Lect & Tip/javascript, jQuery

๋ฆฌ์•กํŠธ Warning: Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>.

by ๋‚ฏ์„ ๊ณต๊ฐ„2019 2021. 2. 27.

๋ชฉ์ฐจ

    ๋ฆฌ์•กํŠธ ํ”„๋ŸฐํŠธ์—”๋“œ ๊ฐœ๋ฐœ ์ค‘์— select๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ jquery๋ฅผ ์“ฐ๋˜ ์Šต์„ฑ๋Œ€๋กœ ์ฝ”๋”ฉ์„ ํ•˜๋‹ค ๋ณด๋ฉด ๋ฐ˜๋“œ์‹œ ๋งŒ๋‚˜๊ฒŒ ๋˜๋Š” ํ”ํ•œ ์˜ค๋ฅ˜ ์ค‘์˜ ํ•˜๋‚˜๊ฐ€ ๋ฐ”๋กœ selected๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ง๋ผ๋Š” ๊ฒฝ๊ณ ๋‹ค.

    Warning: Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>.

    'selected'๋Œ€์‹ ์— select์— defaultValue ๋˜๋Š” value props๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๋Š” ๋œป์ด๋‹ค.

    ์ž˜๋ชป ์“ด ๋ฆฌ์•กํŠธ ์…€๋ ‰ํŠธ ์‚ฌ์šฉ๋ฒ•

    <select id="cons_keep_sel" disabled={modeDisabled}>
      <option
        value=""
        selected={
          companyInfo.cons_keep !== "Y" && companyInfo.cons_keep !== "N"
            ? true
            : false
        }
      >
        ์„ ํƒ์•ˆํ•จ
      </option>
      <option value="Y" selected={companyInfo.cons_keep === "Y" ? true : false}>
        ๊ฐ€๋งน
      </option>
      <option value="N" selected={companyInfo.cons_keep === "N" ? true : false}>
        ํ•ด์ง€
      </option>
    </select>;

    ์•„๋ž˜์™€ ๊ฐ™์ด ์“ฐ๋ฉด ๊ฐ„๋‹จํ•˜๋‹ค.

    <select id="cons_keep_sel" disabled={modeDisabled} value={companyInfo.cons_keep} ref={inpConsKeep}>
      <option value="">์„ ํƒ์•ˆํ•จ</option>
      <option value="Y">๊ฐ€๋งน</option>
      <option value="N">ํ•ด์ง€</option>
    </select>

    ref props๋Š” ํ•„์š”์— ๋”ฐ๋ผ ์‚ฌ์šฉ๋œ ๊ฒƒ์ด๋‹ˆ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.

    ๋งŒ์•ฝ select์—์„œ ๋ฉ€ํ‹ฐํ”Œ ์…€๋ ‰ํŠธ๋กœ ์„ ํƒํ•  ๊ฒฝ์šฐ์—๋Š” value๋ฅผ ๋ฐฐ์—ด๋กœ ์ฃผ๋ฉด ๋œ๋‹ค.

    <select multiple={true} value={['B', 'C']}>

    ์ด ์ ์€ ๊ฑฐ์˜ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ์…€๋ ‰ํŠธ์™€ ๊ฐ™์€ ๊ฐœ๋…์ด๋‹ค.

    ์•„๋ž˜๋Š” reactjs.org์˜ ์˜ˆ์ œ๋‹ค.

    ko.reactjs.org/docs/forms.html

     

    ํผ – React

    A JavaScript library for building user interfaces

    ko.reactjs.org

    class FlavorForm extends React.Component {
      constructor(props) {
        super(props);
        this.state = {value: 'coconut'};
    
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
      }
    
      handleChange(event) {
        this.setState({value: event.target.value});
      }
    
      handleSubmit(event) {
        alert('Your favorite flavor is: ' + this.state.value);
        event.preventDefault();
      }
    
      render() {
        return (
          <form onSubmit={this.handleSubmit}>
            <label>
              Pick your favorite flavor:
              <select value={this.state.value} onChange={this.handleChange}>
                <option value="grapefruit">Grapefruit</option>
                <option value="lime">Lime</option>
                <option value="coconut">Coconut</option>
                <option value="mango">Mango</option>
              </select>
            </label>
            <input type="submit" value="Submit" />
          </form>
        );
      }
    }

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€