컴공과컴맹효묘의블로그

[React] Uncaught TypeError: Cannot read properties of undefined 본문

개발/버그로그

[React] Uncaught TypeError: Cannot read properties of undefined

효묘 2022. 6. 19. 17:59
반응형

리액트를 하다가 setState에서 uncaught typerror가 났다.

보통 초기 state값을 주지 않아서 타입 에러가 나는것인데 내 경우에는 조금 달랐다.

input태그에서 string을 받고 onChange되면 this.func를 호출하는 식인데 이때 함수가 바인딩이 안돼서 에러가 났었다.

// 에러 코드
<div className="flex flex-col text-inherit">
    <input className="custominput my-3" placeholder="E-mail" type="text" name="input_id" value={inputId} onChange={this.handleInputId}/>
    <input className="custominput my-3" placeholder="Password" type="password" name="input_pw" value={inputPw} onChange={this.handleInputPw}/>
</div>
// 에러 해결
<div className="flex flex-col text-inherit">
    <input className="custominput my-3" placeholder="E-mail" type="text" name="input_id" value={inputId} onChange={this.handleInputId.bind(this)}/>
    <input className="custominput my-3" placeholder="Password" type="password" name="input_pw" value={inputPw} onChange={this.handleInputPw.bind(this)}/>
</div>
반응형
Comments