React의 이벤트 핸들러에서 "Cannot read property '〇〇' of undefined"라고 나온다
this 를 사용할 수 없게 되었기 때문에 그 원인을 공유합니다.문제 코드
App.jsxclass App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            status: "No"
        };
    }
    changeState() {
        this.setState({ status: "Yes" });
    }
    render() {
        return (
            <div>
                <button onClick={ this.changeState }>Change!</button>
                <div>{this.state.status}</div>
            </div>
        );
    }
}
이 경우 브라우저에서 실행해 보면 다음과 같이 표시됩니다.
 
이 버튼을 누르면 표시되는 문자가 'Yes'로 바뀔 것입니다만, 변하지 않고 아래와 같은 에러가 발생했습니다.
 
 해결책
render() 함수의 onClick에서 다음과 같이 변경합니다.
App.jsx<button onClick={ this.changeState.bind(this) }>Change!</button>
 원인
ES5의 React.createClass를 사용하면 이벤트 처리기가 자동으로 bind되지만 ES6에서 React.Component를 만들면 이벤트 처리기가 자동으로 bind되지 않는 것 같습니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(React의 이벤트 핸들러에서 "Cannot read property '〇〇' of undefined"라고 나온다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/tenma/items/3ca6c19ec8f90baaf854
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            status: "No"
        };
    }
    changeState() {
        this.setState({ status: "Yes" });
    }
    render() {
        return (
            <div>
                <button onClick={ this.changeState }>Change!</button>
                <div>{this.state.status}</div>
            </div>
        );
    }
}
render() 함수의 onClick에서 다음과 같이 변경합니다.App.jsx
<button onClick={ this.changeState.bind(this) }>Change!</button>
원인
ES5의 React.createClass를 사용하면 이벤트 처리기가 자동으로 bind되지만 ES6에서 React.Component를 만들면 이벤트 처리기가 자동으로 bind되지 않는 것 같습니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(React의 이벤트 핸들러에서 "Cannot read property '〇〇' of undefined"라고 나온다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/tenma/items/3ca6c19ec8f90baaf854
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
Reference
이 문제에 관하여(React의 이벤트 핸들러에서 "Cannot read property '〇〇' of undefined"라고 나온다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/tenma/items/3ca6c19ec8f90baaf854텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)