ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

Route๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์—์„œ ๋ฉ”์ธ ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•˜๋Š” ๊ตฌํ˜„ ๊ณผ์ •์—์„œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด withRouter๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฉ”์„œ๋“œ ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ props๋กœ ๋„˜๊ฒจ ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•˜์˜€๋‹ค.

 

goMain() ๋ฉ”์„œ๋“œ์—์„œ๋Š” ์ธ์ž ๊ฐ’์œผ๋กœ ๋ฐ›์€ ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก history ๊ฐ์ฒด props๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

import React, {Component} from 'react';
import Login from '../Login/Login';
import {withRouter} from 'react-router-dom';

class LoginPage extends Component {

    goMain(path) {
        this.props.history.push(path);
    }

    render() {
        return (
            <>
                <Login goMain={this.goMain}/>
            </>            
        )
    }
}

export default withRouter(LoginPage);

 

๋กœ๊ทธ์ธ ํ™”๋ฉด์—์„œ ์ผ์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๊ฒฝ์šฐ์— ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” props๋กœ ์ „๋‹ฌ ๋ฐ›์€ ๋ฉ”์„œ๋“œ์ธ goMain()์— path ๊ฐ’์„ ์ „๋‹ฌํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์˜๋„ํ•œ ๋ฐ” ์˜€์ง€๋งŒ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

error of this binding

goMain()์—์„œ๋Š” this.props๋ฅผ ํ†ตํ•ด history ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜๊ณ  ์žˆ๋Š”๋ฐ, goMain()์ด ํ˜ธ์ถœ๋œ ์œ„์น˜๋Š” ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์•„๋‹Œ, goMain์„ props๋กœ ์ „๋‹ฌ ๋ฐ›์€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์ด๊ธฐ ๋•Œ๋ฌธ์— this ๋ฐ”์ธ๋”ฉ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ํ•ด๋‹น ๊ฐ์ฒด์— ์ œ๋Œ€๋กœ ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•œ ๋ฌธ์ œ์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

ํ•ด๊ฒฐ

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด goMain() ๋ฉ”์„œ๋“œ ๋‚ด์˜ this๋ฅผ ํ™•์‹คํ•˜๊ฒŒ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์— ๊ท€์†์‹œํ‚ฌ ํ•„์š”๊ฐ€ ์žˆ์—ˆ๋‹ค.

 

์ด๋ฅผ ์œ„ํ•ด Funtion.prototpye.bind() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

 

Function.prototype.bind() - JavaScript | MDN

bind() ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ฐ›๊ฒŒ๋˜๋Š” ์ฒซ ์ธ์ž์˜ value๋กœ๋Š” this ํ‚ค์›Œ๋“œ๋ฅผ ์„ค์ •ํ•˜๊ณ , ์ด์–ด์ง€๋Š” ์ธ์ž๋“ค์€ ๋ฐ”์ธ๋“œ๋œ ํ•จ์ˆ˜์˜ ์ธ์ˆ˜์— ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

 

์ƒ์„ฑ์ž ํ•จ์ˆ˜์ธ constructor() ๋‚ด์—์„œ goMain() ๋ฉ”์„œ๋“œ๋ฅผ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ this์™€ ๋ฐ”์ธ๋”ฉ๋˜๋„๋ก ์ดˆ๊ธฐํ™”ํ•ด์ฃผ์—ˆ๊ณ , ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

ํ•ด๋‹น ๋ฐฉ์‹ ์™ธ์—๋„ ๊ตฌ๊ธ€๋งํ•ด๋ณด๋‹ˆ, ์ด๋ฒคํŠธ props๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์—์„œ๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๊ฐ์‹ธ์ฃผ์–ด ๋ณด๋‚ด์ฃผ๋ฉด ์•ˆ์ •์ ์œผ๋กœ this ๋ฐ”์ธ๋”ฉ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ์„ ํ™•์ธํ•˜์˜€๋‹ค.

import React, {Component} from 'react';
import Login from '../Login/Login';
import {withRouter} from 'react-router-dom';

class LoginPage extends Component {
    constructor(props) {
        super(props);
        this.goMain = this.goMain.bind(this);
    }

    goMain(path) {
        this.props.history.push(path);
    }

    render() {
        return (
            <>
                <Login goMain={this.goMain}/>
            </>            
        )
    }
}

export default withRouter(LoginPage);

 

๊ด€๋ จ ํฌ์ŠคํŒ…

https://choi95.tistory.com/107

 

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ this ๋ฐ”์ธ๋”ฉ ๋ฌธ์ œ

๋ฌธ์ œ li.addEventListener('click', () => { const obj = { image: this.dataset.image, ...JSON.parse(this.dataset.user) } modal.classList.add('active'); thumbnail.src = obj.image; profileImg.src = obj.m..

choi95.tistory.com

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/05   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ