기능적인 로그인 구성 요소를 만들려고합니다. onChange에 대해 댓글을 달았을 때 브라우저에서 아래 오류가 발생합니다.
index.js:1 Warning: You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.
그리고 onChange의 주석 처리를 제거하고 사용자 이름 필드를 시도하면 아래 오류가 발생합니다 .Login.js : 20 Uncaught TypeError : Cannot read property 'inputChangedHandler'of undefined at onChange (Login.js : 20)
그것을 해결하는 방법? import React, {useState, props} from 'react';
const Login = (prop) => {
const [username] = useState('Mr x');
const inputChangedHandler = (event) => {
const updatedKeyword = event.target.value;
// May be call for search result
}
return (
<div>
<h1>Login</h1>
<div className="container">
User Name: <input type="text" name="username" value={username}
// onChange={(event) => this.inputChangedHandler(event)}
/>
Password: <input type="password" name="password" />
<button className="btn btn-success" >Login</button>
</div>
</div>
)
}
export default Login;
당신은 제거 할 필요 this
에서this.inputChangedHandler(event)
그리고 실제로 값을 변경하려면 useState
이것에 setter를 추가해야 합니다.const [username,setUsername] = useState('Rahul Raj');
그리고 이렇게 사용하세요 onChange={(event) => setUsername(event.target.value)}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다