ref - MoonGyeongHyeon/React_Study GitHub Wiki
ref๋ reference๋ฅผ ์๋ฏธํ๋ฉฐ, DOM ์์์ ์ด๋ฆ์ ๋ฌ์์ค๋ค. HTML์ id์ ๋น์ทํ์ง๋ง, id๋ ์ผ๋ฐ DOM ์์์ ํน์ DOM ๋ฉ์๋๋ง ์ฌ์ฉํ ์ ์๋ ๋ฐ๋ฉด, ref๋ DOM ์์์ ์ฌ์ฉํ ์ ์์ ๋ฟ๋ง ์๋๋ผ ์ปดํฌ๋ํธ์์๋ ์ฌ์ฉํ ์ ์๊ณ , ํด๋น ์ปดํฌ๋ํธ๊ฐ ๊ฐ์ง๊ณ ์๋ ๋ฉ์๋์ ๋ณ์๋ค์ ์ฌ์ฉํ ์ ์๋ค๋ ํฐ ์ฐจ์ด๊ฐ ์๋ค.
ref๋ ์๋ฌด ๊ณณ์์๋ ์์ฃผ ์ฌ์ฉํ๋ ๊ฑด ๊ถ์ฅํ์ง ์๋๋ค. ๋น๋ก ref๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ๊ฐ๋ ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ์งค ์ ์๋๋ก ํด์ฃผ์ง๋ง, state์ props๋ก ํด๊ฒฐํ ์ ์๋ ๋ถ๋ถ์์ ref๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , ํด๊ฒฐํ ์ ์๋ ๋ถ๋ถ์์๋ง ref๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ์ ์ง๋ณด์์ ์ข์ ๋ฐฉํฅ์ด๋ค.
- ์ปดํฌ๋ํธ์ ์ํด ๋ ๋๋ง ๋ DOM์ ์ง์ ์ด๋ ํ ์ฒ๋ฆฌ๋ฅผ ํด์ผํ ๊ฒฝ์ฐ.
- ํฐ ํ๋ก์ ํธ์ React ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ(๋ค๋ฅธ ํ๋ ์์ํฌ์ ํผ์ฉ).
ref๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ ๋ฌธ์์ด Attribute ์ฌ์ฉ
, ์ฝ๋ฐฑ ํจ์ ์ฌ์ฉ
์ผ๋ก ๋๋ ์ ์๋ค.
-
๋ฌธ์์ด Attribute ์ฌ์ฉ
์ด ๋ฐฉ๋ฒ์ outdated ๋์์ง๋ง ์ฐธ๊ณ ์ฉ์ผ๋ก ๋ฐฐ์๋ณด๋๋ก ํ์.
class Hello extends React.Component { render() { return ( <div> <input ref="myInput"> </input> </div> ) } componentDidMount() { this.refs.myInput.value = "Hi, I used ref to do this"; } } ReactDOM.render( <Hello/>, document.getElementById('app') );
render() ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋ (๋ ๋๋ง ๋ ๋), input ํ๊ทธ์ ref ๊ฐ์ ์ค์ ํด์ค๋ค. (ref="refName" ํ์)
๊ทธ ํ, componentDidMount() ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋์์ ํด๋น ํ๊ทธ(ref ๊ฐ์ ํตํด ์ฐธ์กฐ)์ value ๊ฐ์ ๋ณ๊ฒฝํ๋ค. ๋ฌธ์์ด ํ์์ผ๋ก ๋ง๋ ref๋
this.refs.refName
์ผ๋ก ์ ๊ทผํด์ผ ํ๋ค.-
(!) ์ปดํฌ๋ํธ์ ๋ ๋๋ง์ด ๋ชจ๋ ๋๋ ํ์์ผ refs๊ฐ ์ ์๋๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋ ๋๋ง์ด ๋ชจ๋ ๋๋ ํ์ refs๋ฅผ ์ฌ์ฉํด์ผ ํ๋ฉฐ, ๋ง์ฝ ๊ทธ ์ ์ ์ฌ์ฉํ๋ ค ํ๋ค๋ฉด, ์๋์ ๊ฐ์ด refs ์ดํ์ ๊ฐ์ด ์ ์๋์ด ์์ง ์๋ค๋ ์ค๋ฅ๋ฅผ ๋ณผ ์ ์๋ค.
-
-
์ฝ๋ฐฑ ํจ์ ์ฌ์ฉ
class Hello extends React.Component { render() { return ( <div> <input ref={ref => this.input = ref}> </input> </div> ) } componentDidMount() { this.input.value = "I used ref to do this"; } } ReactDOM.render( <Hello/>, document.getElementById('app') );
render() ๋ฉ์๋์์, ํจ์๋ฅผ ์ด์ฉํ์ฌ ref๋ฅผ ์ค์ ํ๋ค. ํจ์๋ ์ด๋ค ๋ณ์๊ฐ ref๋ก ์ฌ์ฉ๋ ์ง ์ง์ ์ ํ๋ค.
ref๋ฅผ ์ด์ฉํ ๋
this.refs
๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , ์์์ ์ง์ ํ ๊ฐ(this.input)์ ํธ์ถํ๋ฉด ๋๋ค.
class Hello extends React.Component {
handleClick() {
this.textBox.input.value = "I used ref";
}
render() {
return (
<div>
<TextBox ref={ref=>this.textBox = ref}/>
<button onClick={this.handleClick.bind(this)}>Click Me</button>
</div>
)
}
}
class TextBox extends React.Component {
render() {
return(
<input ref={ref => this.input = ref}>
</input>
)
}
}
ReactDOM.render(
<Hello/>,
document.getElementById('app')
)
Hello
์ปดํฌ๋ํธ ์์๋ TextBox
์ปดํฌ๋ํธ์ button ํ๊ทธ๊ฐ ์กด์ฌํ๋ค. TextBox
์ ref๋ฅผ this.textBox
๋ก ์ค์ ํด๋ ์ํ์ด๋ค. TextBox
๋ input ํ๊ทธ๋ฅผ ํ๋ ๊ฐ์ง๊ณ ์๋๋ฐ, ๊ทธ ํ๊ทธ์ ref๋ this.input
์ ํตํด ์ ๊ทผํ ์ ์๋ค. button ํ๊ทธ๋ ํด๋ฆญํ ๊ฒฝ์ฐ, this.textBox.input.value
์ ๊ฐ์ "I used ref" ๋ก ์ค์ ํ๋ ์ฝ๋ฐฑํจ์๊ฐ ์คํ์ด ๋๋ค.
์๋ ๋จ์ํ ์ด๋ ๊ฒ ์ฌ์ฉํ ์ ์๋ค๋ ๋ฐฉ๋ฒ์ ์ ์ํ ๊ฒ ๋ฟ, ์ ๋ฐ ์์ ์ข์ง ๋ชปํ ํ๋ก์ฐ๋ก ์ฌ์ฉํด์ ์๋๋ค.
ref๋ฅผ ์ฌ์ฉํด์ผ๋ง ๊ตฌํํ ์ ์๋ ์ฌ๋ก๋ฅผ ์์๋ณด๋๋ก ํ์.
์๋ฅผ ๋ค์ด, input๊ณผ button์ด ์๊ณ button์ ๋๋ฅด๋ฉด input์ ์ด๊ธฐํํ๊ณ focusํด์ผ ํ ๋๋ ref๋ฅผ ์ฌ์ฉํด์ผ๋ง ํ๋ค.
class Hello extends React.Component {
handleClick() {
this.input.value = "";
this.input.focus();
}
render() {
return (
<div>
<input ref={ref=> this.input = ref}/>
<button onClick={this.handleClick.bind(this)}>Click Me</button>
</div>
);
}
}
ReactDOM.render(
<Hello/>,
document.getElementById('app')
);
Javascript DOM ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ input box์ ํฌ์ปค์ฑ์ ํ ์ ์๋ค.